I'm using AlarmManager
to schedule anywhere between 1 and 35 alarms (depending on user input). When the user requests to schedule new alarms, I need to cancel the current alarms, so I create all of my alarms with the same requestCode, defined in a final
variable.
// clear remaining alarms
Intent intentstop = new Intent(this, NDService.class);
PendingIntent senderstop = PendingIntent.getService(this,
NODIR_REQUESTCODE, intentstop, 0);
am.cancel(senderstop);
// loop through days
if (sched_slider.getBooleanValue())
for (int day = 1; day < 8; day++) {
if (day == 1 && sun.isChecked())
scheduleDay(day);
if (day == 2 && mon.isChecked())
scheduleDay(day);
if (day == 3 && tue.isChecked())
scheduleDay(day);
if (day == 4 && wed.isChecked())
scheduleDay(day);
if (day == 5 && thu.isChecked())
scheduleDay(day);
if (day == 6 && fri.isChecked())
scheduleDay(day);
if (day == 7 && sat.isChecked())
scheduleDay(day);
}
...
public void scheduleDay(int dayofweek) {
Intent toolintent = new Intent(this, NDService.class);
toolintent.putExtra("TOOL", "this value changes occasionally");
PendingIntent pi = PendingIntent.getService(this,
NODIR_REQUESTCODE, toolintent, 0);
calendar.set(Calendar.DAY_OF_WEEK, dayofweek);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 7, pi);
}
Here, if the user has sun
(which is a CheckBox) checked, it will schedule an alarm to run every Sunday at hour
and minute
. You can see that every alarm created this way has the same requestCode, but the TOOL
extra changes sometimes for each alarm.
However, in my testing, when the alarm goes off and my Service runs, the extras from the Intent are now null
. This question suggests that using PendingIntent.FLAG_CANCEL_CURRENT
will solve this, but wouldn't that cancel the other PendingIntents?
In short:
Can someone explain how PendingIntents work, in reference to creating multiple ones with the same requestCode and different extras? What flags (if any) should I be using?