Duplicate intentions after updating the application. Do alarms persist?

I have an error when I get duplicates after updating. The change I made was to start listening to MY_PACKAGE_REPLACED and re-register the signal below, because I assumed that when the application was updated, alarms were killed. However, I do get duplicate translations of the "TriggerPulse" defined in the code below.

The code below is the only code that sets the alarm. However, it is called from several places, but as far as I can compile from documents, this code should be idempotent (IntentRequestCodes.PULSE_SERVICE is a static integer set to 1). Is it true to me 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 this mean that the pendingIntent created by the application to update is simply not considered a match with the pendingIntent created by the old deprecated application, resulting in the forwarding of the alarm anyway? If so, I can easily solve the problem simply by not recreating the alarm on MY_PACKAGE_REPLACED. However, several resources there offer re-registration of these events at this event, which leads to the fact that at some point this can happen in Android. If so, is there an Android version where this change has been changed?

+5
source share
1 answer

Try to cancel the previous alarm before adding it again, so even if it was added earlier, there were no problems.

  PendingIntent alarmIntent = PendingIntent.getBroadcast(CONTEXT, MY_ID, MY_INTENT, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmMgr = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE); alarmMgr.cancel(alarmIntent); 
+3
source

Source: https://habr.com/ru/post/1243501/


All Articles