I have a BroadcastReceiver
which reschedules alarms on events such as booting and time change. But when the time is past the trigger time of the alarm (For example, when the user manually changes the time from the settings), AlarmManager
fires the alarm immediately before I can add a day to reschedule my alarm. How can I avoid this?
I'm currently using the set and add methods of the Calendar
to schedule the alarms.
for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; dayOfWeek++) {
if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek >= nowDay &&
!(dayOfWeek == nowDay && alarm.timeHour < nowHour) &&
!(dayOfWeek == nowDay && alarm.timeHour == nowHour && alarm.timeMinute <= nowMinute)) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
alarmSet = true;
break;
}
}
if (!alarmSet) {
for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; dayOfWeek++) {
if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek <= nowDay) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.add(Calendar.WEEK_OF_YEAR, 1);
break;
}
}
}
It is also stated in the docs:
If the stated trigger time is in the past, the alarm will be triggered immediately.
How can this behaviour be altered?