Parashar
Parashar

Reputation: 85

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?

Upvotes: 1

Views: 1792

Answers (2)

cesards
cesards

Reputation: 16339

You could also use:

final PendingIntent pendingIntent = PendingIntent.getService(
    contextWeakReference.get(),
    0,
    notificationIntent,
    PendingIntent.FLAG_CANCEL_CURRENT
);

As the documentation says:

   /**
     * Flag indicating that if the described PendingIntent already exists,
     * the current one should be canceled before generating a new one.
     * For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>You can use
     * this to retrieve a new PendingIntent when you are only changing the
     * extra data in the Intent; by canceling the previous pending intent,
     * this ensures that only entities given the new data will be able to
     * launch it.  If this assurance is not an issue, consider
     * {@link #FLAG_UPDATE_CURRENT}.
     */
    public static final int FLAG_CANCEL_CURRENT = 1<<28;


    /**
     * Flag indicating that if the described PendingIntent already exists,
     * then keep it but replace its extra data with what is in this new
     * Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>This can be used if you are creating intents where only the
     * extras change, and don't care that any entities that received your
     * previous PendingIntent will be able to launch it with your new
     * extras even if they are not explicitly given to it.
     */
    public static final int FLAG_UPDATE_CURRENT = 1<<27;

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007339

Use FLAG_UPDATE_CURRENT when creating your PendingIntent.

Upvotes: 5

Related Questions