repeating alarm not cancelling
I have a repeating alarm set, my problem is that after cancelling it doesn't cancel (I'm checking this with Log.v()
This is how I create the alarm (in an IntentService)
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentToFire = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
private void rescheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, TIME_TO_REFRESH);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CHECK_TIME, alarmIntent);
}
Then in an Activity I have a button on upon it's click it calls this code
private OnClickListener btnCloseApplicationListener = new OnClickListener() {
public void onClick(View v) {
intentToCancel = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
alarmIntent = P开发者_Python百科endingIntent.getBroadcast(v.getContext(), 0, intentToCancel, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(alarmIntent);
finish();
}
};
After the finish() is executed I keep seeing the logs I have in DDMS window. How can I cancel it? Thanks in advance!
try using same context for the intent. The intents have to match and I think your problem is that you are using different context when trying to cancel the alarm.
alarmIntent = PendingIntent.getBroadcast(this, 0, intentToCancel, 0);
精彩评论