gpt4 book ai didi

android - Alarmmanager 每隔一段时间显示 toast

转载 作者:行者123 更新时间:2023-11-29 02:07:41 26 4
gpt4 key购买 nike

您好,我是 Java 和 android 的新手,我正在尝试将 alarmmanager 与计时器一起使用,以便在按下 startButton 后每 10 秒显示一次 toast 。 10 秒后什么也没有发生,我不确定为什么。

你能看看我的代码并帮助我找出并解决问题吗?

谢谢。

变量 - journeycount = chronometer,pendingIntent mAlarmSender

主类 - SafeDrive3Activity

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

journeycount = (Chronometer) findViewById(R.id.journeycount);
// Watch for button clicks.
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(mStartListener);

resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(mResetListener);

}

View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {

int stoppedMilliseconds = 0;

String chronoText = journeycount.getText().toString();
String array[] = chronoText.split(":");

if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}

journeycount.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
journeycount.start();
startButton.setText("Driving..."); // changes button text value to 'Driving...'
startButton.setOnClickListener(mStartListener);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
// add 10 seconds to calendar object
cal.add(Calendar.SECOND, 10);
mAlarmSender = PendingIntent.getBroadcast(SafeDrive3Activity.this,
0, new Intent(SafeDrive3Activity.this, MyTimeReceiver.class), 0);

// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), mAlarmSender);

}
};

View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
Button startButton;
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(mStartListener);
journeycount.setBase(SystemClock.elapsedRealtime());

startButton.setText("Start Driving");
journeycount.setText("00:00");
journeycount.stop();

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(mAlarmSender);


}
};


};

第二类:

public class MyTimeReceiver extends BroadcastReceiver 

{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "HOUR NOTIFICATION", Toast.LENGTH_LONG).show();

}
}

list .xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Safe"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SafeDrive3Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name="MyTimeReceiver"></receiver>
</activity>
</application>
</manifest>

最佳答案

我认为您应该在这种情况下考虑采用不同的方法。为什么不使用仅启动 toast 消息的计时器?

Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Toast.makeText(SafeDrive3Activity.this, "Whatever", Toast.LENGTH_LONG).show();
}
}, 0, 10000);

关于android - Alarmmanager 每隔一段时间显示 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com