我試圖停止重複報警。
我已經找遍了整個網絡的解決方案,但我的代碼似乎罰款...
不過它不會停止報警...... 這裏的代碼一點點(FM是一個文件,寫對象)
alarmManager.cancel不能正常工作
public void StartAlarm() {
Intent intent = new Intent(getApplicationContext(), AlarmReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar alarm = Calendar.getInstance();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.set(Calendar.HOUR_OF_DAY, 15);
alarm.set(Calendar.MINUTE, 0);
long alarmCal=alarm.getTimeInMillis();
if (alarmCal<=new Date().getTime()) {
alarm.add(Calendar.DAY_OF_MONTH, 1);
}
am.setRepeating(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender);
FM.Write("startAlarm()");
}
public void StopAlarm() {
if (isAlarmRunning()) {
Intent intent = new Intent(getApplicationContext(), AlarmReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
FM.Write("alarm canceled");
isAlarmRunning();
}
public boolean isAlarmRunning() {
Intent intentToCheck = new Intent(getApplicationContext(),AlarmReciever.class);
boolean alarm = PendingIntent.getBroadcast(getApplicationContext(), 0, intentToCheck,
PendingIntent.FLAG_NO_CREATE) != null;
FM.Write("alarm = "+alarm);
if (!alarm) {
return false;
}
else
return true;
}
謝謝!
沒有工作。還是一樣的... – 2012-03-18 20:22:14
代替0有一些數字說1234 – 2012-03-18 20:25:37
你有沒有卸載應用程序之前檢查它這次 – 2012-03-18 20:26:21