How to cancel all alarms set in AlarmManager?

How to cancel all alarms set in AlarmManager? :

I am aware that this will not help everyone, but maybe it could help some people that could be in a similar situation.
Seems that AlarmManager does not have a cancelAll() method or something, and it can be a trouble some of us.
After searching some info about the AlarmManager, I came up with the idea of having random IDs in the PendingIntent, so that the notifications could be treated as different ones (as I state in the original question).
As I previously said, this will not help anyone, but because I am using a Database, I can achieve this.
Instead of using random generated IDs, I changed it so that the ID is my saved alarm (the one in the database) ID, and because one of the main rules of SQL is that no record should have the same ID, it guarantees me that all notifications will have a different ID, while having the possibility of canceling them. This is the finished code:

for (int i = 0; i < dates.size(); i++) {
            TaskItem current = dates.get(i); //dates is the arraylist where I storage all my alarms
            long millis = current.getDateInMillis();

                intent = new Intent(getApplicationContext(), NotificationReceiver.class);
                Bundle b = new Bundle();
                b.putInt("ID", current.getID());
                b.putString("Title", current.getTitle());
                b.putString("Description", current.getDescription());
                b.putInt("Tag", current.getTag());
                b.putInt("NotID", current.getID());

                pi = PendingIntent.getBroadcast(getApplicationContext(), current.getID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
                manager = (AlarmManager) getSystemService(ALARM_SERVICE);
                manager.setExact(AlarmManager.RTC_WAKEUP, millis, pi);


AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                getApplicationContext(), 1, myIntent, 


I know you said without PendingIntent, but what you could do is set up some logic where, after that item has been selected, you launch the above code to cancel an alarm that maybe already set, and then start a new one after that.

Leave a Comment