7

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.

Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36
  • did replacing `PendingIntent.getActivity` with `PendingIntent.getBroadcast` and `PendingIntent.FLAG_CANCEL_CURRENT` with 0 work? – Sufiyan Ghori Dec 24 '14 at 14:14
  • using getBroadcast, none of the alarms fire (bcoz my alarm calls an activity, not a broadcast)..... Using FLAG_CANCEL_CURRENT doesnt work... I have tried it. – Hirak Chhatbar Dec 24 '14 at 17:06
  • Does this answer your question? [Android AlarmManager: is there a way to cancell ALL the alarms set?](https://stackoverflow.com/questions/17569896/android-alarmmanager-is-there-a-way-to-cancell-all-the-alarms-set) – Bink May 18 '23 at 18:13

4 Answers4

7

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much for the reply. I got the concept. But I have to set multiple alarms, so having same second parameter to getActivity() will replace the old alarm instead of creating a new one. How shall I overcome this? – Hirak Chhatbar Dec 24 '14 at 17:08
  • to set multiple alarms, just use multiple values for the variable code inside a loop. – Sufiyan Ghori Dec 24 '14 at 17:10
  • @HirakChhatbar: "How shall I overcome this?" -- keep track of the codes that you are using, then use those same codes when creating the `PendingIntent` to replace or cancel the alarm. – CommonsWare Dec 24 '14 at 17:11
  • @CommonsWare - I ended up creating a separate activity for cancelling the alarms where I would pass the code for the alarm that I want to delete and then delete it with that code..... Thank you for the help – Hirak Chhatbar Dec 24 '14 at 17:56
  • The extras that I pass to my intent that I send to the pendingIntent change when I recreate the pendingIntent. Is this the reason why my alarms wouldn't cancel? – Sagar Aug 20 '17 at 09:12
  • @Sagar: I cannot answer that. I suggest that you ask a separate Stack Overflow question, where you provide a [mcve] showing what you are doing and what your specific symptoms are. – CommonsWare Aug 21 '17 at 11:42
1

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.

Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
1

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

You can see my solution here

Community
  • 1
  • 1
extmkv
  • 1,991
  • 1
  • 18
  • 36
0

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);
Dayanand Waghmare
  • 2,979
  • 1
  • 22
  • 27