Search code examples
javaandroidalarmmanageralarmandroid-alarms

Alarm manager: repeating alarm not always fires


I'm trying to develop a kind of reminders system, so user can enter days for reminders (MON, TUE...) and times on which this reminder will fire (only hour and minutes). It can be any day of the week or multiple days of the week. Here is the code for setting these reminders:

        final AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        for (final Day day: reminder.getDays()) {
            for (final ReminderTime reminderTime: reminder.getTimes()) {
                final Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, reminderTime.getHour());
                calendar.set(Calendar.MINUTE, reminderTime.getMinute());
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.DAY_OF_WEEK, day.getCalendarDay());

                if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
                    // final long daysDifference = DateUtils.getDaysDifference(calendar.getTimeInMillis(), System.currentTimeMillis());
                    // calendar.add(Calendar.DAY_OF_YEAR, (int) (daysDifference + 1));
                    calendar.add(Calendar.DAY_OF_YEAR, 7);
                }

                final Intent intent = createReminderIntent(context, reminder.getReminderType(), reminderTime, day);
                final PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, sender);
            }
        }

The alarm I recieve later in custom BroadcastReceiver. The problem is that the reminder is firing from time to time and sometimes not, seems to me that something is woring with the days. I'm wondering were is the bug or what I'm doing wrong?


Solution

  • When calling PendingIntent.get*(...) with the same Intent and request code the same instance of PendingIntent is returned.

    The AlarmManager can have only one rule associated for one PendingIntent which means that only the last alarm set by AlarmManager.setRepeating(...) in your code is actually active. The others got overwritten by this last rule.

    One way to differentiate PendingIntents with the same Intent is to use a different request code for each. When passed to AlarmManager these will trigger individual alarms as expected.

    Sadly there's no way to cancel multiple alarms defined e.g. by base Intent so you have to either

    • keep all your PendingIntent instances used originally to schedule the alarms
    • keep all the request codes and reconstruct said PendingIntents
    • or use similar mechanism.

    Then you need to call AlarmManager.cancel(pi) with each of these PendingIntents individually to cancel associated alarms.