Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT

My app sets a repeating alarm on user interaction, it might change the interval time set for the broadcast with Alarm Manager.
There is not much in the way of extras.
Is the update or cancel flag better in this case?

Thanks

like image 851
Dory Avatar asked Dec 25 '12 09:12

Dory


People also ask

What is FLAG_UPDATE_CURRENT?

FLAG_UPDATE_CURRENT. Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

What is a PendingIntent?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

What is PendingIntent Flag_immutable?

FLAG_IMMUTABLE : Indicates the Intent inside the PendingIntent cannot be modified by other apps that pass an Intent to PendingIntent.send() . An app can always use FLAG_UPDATE_CURRENT to modify its own PendingIntents. Prior to Android 12, a PendingIntent created without this flag was mutable by default.

What is request code in pending intent?

1- requestCode is used to get the same pending intent later on (for cancelling etc) 2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent.


2 Answers

Never use FLAG_CANCEL_CURRENT with PendingIntents that you use when setting alarms. If you want to reschedule the alarm for a different time you don't need any flag at all; just create a duplicate PendingIntent with flags of zero and then use it to set() an alarm: this will implicitly cancel the existing alarm and then set it for the newly-specified time. If you used FLAG_CANCEL_CURRENT when you created the new PendingIntent, though, it breaks the Alarm Manager's ability to recognize that it's "the same" as the now-canceled PendingIntent, and you wind up with the old one hanging around, undeliverable, taking up memory and CPU. I've seen apps with this bug rack up literally hundreds of stale alarms in the system, enough to be a noticeable performance and memory-usage hit.

If you just want to change the extras without actually rescheduling the existing alarm, that is what FLAG_UPDATE_CURRENT is for. If you want to reschedule, don't use any flags at all.

like image 94
ctate Avatar answered Sep 24 '22 11:09

ctate


If you are not using extras, you don't have to specify any of those flags. They only change how the systems handles extras with a PendingIntent: replace the ones in the current matching (cached by the system PendingIntent) or cancel it and create a new one. Refer to the documentation for details: http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT

like image 43
Nikolay Elenkov Avatar answered Sep 24 '22 11:09

Nikolay Elenkov