Cancel all the alarms set using alarmmanager

9.9k views Asked by At

I want to cancel all the alarms that are set. I have searched a lot and tried a lot of things but nothing worked. When I set an alarm for say after 2 minutes and then I cancel it, it will still fire after 2 minutes.

The method used to create the alarms:

Intent intent = new Intent(c, AlarmReceiver.class);
intent.putExtra("task", task);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(c, code, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager am = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

This is to cancel alarms:

Intent intent = new Intent(c, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager am = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);

Any help will be appreciated.

4

There are 4 answers

6
CommonsWare On BEST ANSWER

When you schedule your alarms, your second parameter to getActivity() is code and when you try to cancel your alarms, your second parameter to getActivity() is 0. This will only successfully cancel an alarm whose code was 0.

If you want to consistently cancel the alarms, you need to create equivalent PendingIntents, which means the same Intent object (with the same action, component, data, flags and other attributes) and the same code. It's worth mentioning that the Context you used to create the Intent does not need to be the same for cancellation to see it as the same.

0
Sufiyan Ghori On

You are setting the alarm with request code (second parameter for getActivity) equals to code, while when you are cancelling the alarm, the second parameter is 0. If the value of code is not 0 the alarm wouldn't be cancelled.

In order to fire and cancel multiple alarms, you need to use unique value for requestCode (second parameter) in PendingIntent.getActivity

See this code that will set 3 Alarms using three unique request Codes,

for (int requestCode = 1; requestCode <= 3; requestCode++) {
    PendingIntent pendingIntent = PendingIntent.getActivity(c, requestCode, intent,PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

You can cancel the Alarm using the same requestCode (and other parameters) that were used to set it.

The other parameters which are needed to matched are:

  • The "action" in the Intent must be the same (or both null). Otherwise they do not match.
  • The "data" in the Intent must be the same (or both null). Otherwise they do not match.
  • The "type" (of the data) in the Intent must be the same (or both null). Otherwise they do not match.
  • The "package" and/or "component" in the Intent must be the same (or both null). Otherwise they do not match. "package" and "component" fields are set for "explicit" Intents.
  • The list of "categories" in the Intent must be the same. Otherwise they do not match.

Please see this answer for more details.

0
extmkv On

To cancel an alarm you need no re-create the same PendingIntent and pass the same request code.

You can see my solution here

0
Dayanand Waghmare On

When you cancel alarm its intent must be same with intent which used for set alarm .

so just add following 2 line

intent.putExtra("task", task);

intent.putExtra("id", id);

So your final code will be

Intent intent = new Intent(c, AlarmReceiver.class);
intent.putExtra("task", task);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(c, code, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);