0

Is it possible to change the Intent and it's extras each time AlarmManager is triggered?

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
atanti
  • 57
  • 7

1 Answers1

0
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

Yes everytime you need to cancel the current one and launch the new one if you want to make any changes in Intent like data extras, action , component anything.

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23
  • Will this run the new intent once it is fired, or do I need to call the method again ? – atanti Sep 15 '16 at 08:39
  • How do I call the method as soon as the alarm fires ? – atanti Sep 15 '16 at 08:43
  • When you have to cancel the previous one so definitely you need to give call to method again to create new one. When you are firing alarm in that part of code give call to function. Try this hope it would help I am not much sure about this – Preetika Kaur Sep 15 '16 at 08:54