i have set some alarm like:
public void SetAlarm(Context context, int tag, long time){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
i.putExtra("position", tag);
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute
}
now for some reason i want to update the Intent i
of a triggered alarm. i have the id(tag) for identify the desire alarm. how can i do that ?
If all you want to do is change the extras in the Intent
, you can do it like this:
Intent i = new Intent(context, Alarm.class);
// Set new extras here
i.putExtra("position", tag);
// Update the PendingIntent with the new extras
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Otherwise, if you want to change anything else in the Intent
(like the action, or component, or data), you should cancel the current alarm and create a new one like this:
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
// Extras aren't used to find the PendingIntent
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i,
PendingIntent.FLAG_NO_CREATE); // find the old PendingIntent
if (pi != null) {
// Now cancel the alarm that matches the old PendingIntent
am.cancel(pi);
}
// Now create and schedule a new Alarm
i = new Intent(context, NewAlarm.class); // New component for alarm
i.putExtra("position", tag); // Whatever new extras
pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
// Reschedule the alarm
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute