RushedPotato
RushedPotato

Reputation: 301

How to cancel all alarms set in AlarmManager?

I have an app where I use the AlarmManager, and I add Alarms based on some items in my database, however, I want to cancel all the previously set alarms so that the app won't set them again, and thus, creating a lot of duplicated notifications.
Is there a way that I can just cancel all the alarms without having the PendingIntent id?, I generate it randomly and it would be impossible to figure out which are the IDs.

Upvotes: 0

Views: 473

Answers (2)

RushedPotato
RushedPotato

Reputation: 301

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());
                intent.putExtras(b);


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

        }

Upvotes: 1

beastlyCoder
beastlyCoder

Reputation: 2401

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

alarmManager.cancel(pendingIntent);

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.

Upvotes: 0

Related Questions