Search code examples
androidandroid-intentalarmmanager

How do I make changes to an AlarmManager alarm?


I am using AlarmManager in my app to set an alarm at an appropriate time. I have multiple alarms in my app so every time the user saves an alarm, I find which alarm should be played next time and pass the ID of that alarm as an intent's extra. Here is the code I use for that:

Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("alrmId", finalAlr);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 56, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + (finalAlrDay * 24 * 60 * 60 * 1000) + (finalAlrHr * 60 * 60 * 1000) + (finalAlrMin * 60 * 1000) + (finalAlrSec * 1000)), pendingIntent);

Here, I cancel if there is any old alarm set and then add new one. All the alarms play at the right time but the problem is that the alrmId value that I set in intent.putExtra always remains the same as when I set it for the first time.

For example, if I set an alarm for the first time and at that time alrmId is set to be '1' then it'll always stays the same no matter what value I put in after that. I have tried debugging it and I made sure that intent.putExtra("alrmId", finalAlr) is putting in the right value so that is not the problem. What is the problem?


Solution

  • Use FLAG_UPDATE_CURRENT when creating your PendingIntent.