Overwrite existing alarm
Asked Answered
H

1

5

I create an alaram in my app which is calling a BroadcastReceiver to setup notifications ever day with this code:

Intent intent = new Intent(Benachrichtigung.CUSTOM_INTENT);
PendingIntent pendingIntent  = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);

alram = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alram.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), (24 * 60 * 60 * 1000), pendingIntent);

Now I want that the user can set the time for the norification so I have to call calendar.set with the new value. How can I overwrite the existing alarm with a new one?

Huckster answered 19/6, 2013 at 19:49 Comment(9)
What problem are you having exactly? Is it not overwriting it now?Zacarias
If I call exactly this code with an other hour for example calendar.set(Calendar.HOUR_OF_DAY, 9) does it overwrite the old alarm automatically? So is the old alarm with 8 deleted and the new with 9 created automatically?Huckster
Have you tested it yet to see what happens?Zacarias
No because I don't know how I should test it. Can I view anywhere which alarms are set in the system?Huckster
Well, what I would suggest is using a time that will trigger in a couple of minutes then another that would trigger a couple minutes after that and see if it works as expected. I know giving you the answer would be easier but not as helpful :)Zacarias
Okay I tested it and come to the result that it overwrites the old alarm automaticlly. Is that the result that you have expected?Huckster
Its what I expected but more importantly is it what YOU expected?Zacarias
I thought that maybe two different alarms are created because I call the same function two times. Of cause I can create two different alarms with different intents in my app. Is the same intent in this example the reason for the system to overwrite an existing alarm?Huckster
Yes. I have posted an answer that hopefully explains itZacarias
Z
11

To cancel or update an alarm using AlarmManager the Intent must match using the filterEquals. So basically, you re-create the PendingIntent the same as you did for the original and AlarmManager will see that they are the same. This includes the REQUEST_CODE, INTENT_ACTION, and INTENT_DATA (I may be missing something there but those are important.

Note:

EXTRAS are not used in comparing the two Intents.

So if the two Intents are equal then the first will be overwritten. When I have more time I can try and find a resource to better explain that.

According to the Intent Docs filterEquals

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

Zacarias answered 19/6, 2013 at 20:47 Comment(4)
Great answer thanks for your help! The way that I tested it with you was great to learn a bit more about android! Thanks for that.Huckster
You are welcome! Glad I could help. I try to pass on what I have learned from great SO members such as Sam, prosperk, pragnani, commonsware, and others. Which is Explaining why/how things work is often much more helpful than simply providing code :)Zacarias
Just to be clear, Context doesn't matter? I have different parts of my app in different classes calling my AlarmUtil static function of setAlarm, and I'm worried that I might be just creating multiple alarms. Nor the alarm duration?Incoordinate
@Incoordinate correct, the context won't cause it to cause it to create a new alarm. I do just that because I need to canccel, delete, update alarms in different classes, as well.Zacarias

© 2022 - 2024 — McMap. All rights reserved.