I have a bug where I'm getting duplicate intents after an update. The change I made was to start listening to the MY_PACKAGE_REPLACED, and re-register the alarm below, because I assumed that alarms were killed when the application was updated. However, I am, indeed, getting duplicates of the "TriggerPulse" broadcast defined in the code below.
The code below is the only code that is setting the alarm. It is, however, being called from multiple places, but as far as I can gather from docs, this code should be idempotent (IntentRequestCodes.PULSE_SERVICE is a static integer set to 1). Is it correct of me to assume that this code is idempotent?
final Context applicationContext = context.getApplicationContext();
Intent intent = new Intent("TriggerPulse");
final int flags = 0;
PendingIntent pendingIntent =
PendingIntent.getBroadcast(applicationContext, IntentRequestCodes.PULSE_SERVICE, intent, flags);
AlarmManager alarmMgr = (AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);
final int triggerAtMilliseconds = 0; // run first time immediately
alarmMgr.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerAtMilliseconds,
AlarmManager.INTERVAL_HALF_HOUR,
pendingIntent
If it is idempotent under normal circumstances, does that mean the pendingIntent that updated app is creating is simply not considered to be a match of the pendingIntent that the old legacy app is creating, causing the alarm to be re-scheduled anyway? If this is so, I can easily solve the problem simply not re-creating the alarm on MY_PACKAGE_REPLACED. However, multiple resources out there suggest that one needs to re-register these on that event, which leads be to believe that this was maybe the case in Android at some point. If so, is there a cutoff Android version where this behaviour was changed?
Try cancelling the previous alarm before adding it again, so even if it was added before, there wouldn't be any problem.
PendingIntent alarmIntent = PendingIntent.getBroadcast(CONTEXT, MY_ID, MY_INTENT, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(alarmIntent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With