I'm setting an alarm like this:
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingEvent);
I'm interested in removing all the alarms that where previously set, clearing them.
Is there a way for me to do that or to get all the alarms that are currently set so that I can delete them manually ?
You need to create your pending intent and then cancel it
You don't have to keep reference to it. Just define a new PendingIntent like exactly the one that you defined in creating it.
For example:
if I created a PendingIntent to be fired off by the AlarmManager like this:
Then somewhere in your other code (even another activity) you can do this to cancel:
The important thing here is to set the PendingIntent with exactly the same data and action, and other criteria as well as stated here http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29
To cancel an alarm you need to re-create the same PendingIntent and pass the same request code.
So, I have created an AlarmUtils to help me saving all the request codes in the preferences to cancel it in the future if it is needing. All you need is to use the following class and call the following methods:
addAlarm
to add a new alarm and pass a request code.cancelAlarm
to remove an alarm, you need to re-create the sameIntent and pass the same request code.
hasAlarm
to verify if that alarm as added, you need to re-create the same Intent and pass the same request code.cancelAllAlarms
to remove ALL alarms setted.My AlarmUtils