0

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?

yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • You set the same request code (zero) for all the pending intents. The last one replaces all who came before it. Using variable request code, should fix it. Read [`PendingIntent` class overview](http://developer.android.com/reference/android/app/PendingIntent.html) to understand how this works. – Eugen Pechanec May 19 '15 at 18:52
  • By variable request code you mean always new request code? I know how this `PendingIntent` works, however I don't understand why zero request code is not valid for such case? I thought the flag I'm setting in the last parameter defines if it will be replaced, updated or cancelled. – yyunikov May 19 '15 at 19:02
  • It is valid but you want to plan the same intent multiple times. Therefore you need to generate different pending intents for each one repeating cycle. One way to ensure that is using different request codes. – Eugen Pechanec May 19 '15 at 19:04
  • Thank you for your response, I will try and let you know later if it will work. – yyunikov May 19 '15 at 19:05
  • One more question, for cancelling all these alarms, I will need the same request codes for PendingIntent which I will use here? – yyunikov May 19 '15 at 19:22
  • Afraid so, you'll have to keep references to the pending intents or the request codes so you can reconstruct them and then call `AlarmManager.cancel(pi)` for each of those. I don't know of any way around this. – Eugen Pechanec May 19 '15 at 19:24
  • Thanks for your help, seems that was the problem. You can post your answer to the question so I can accept it as the best one. – yyunikov May 19 '15 at 20:33

2 Answers2

1

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.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0
Calendar Calendar_Object = Calendar.getInstance();

    Calendar_Object.set(Calendar.DAY_OF_WEEK, 6);


    Calendar_Object.setTimeInMillis(System.currentTimeMillis());
    Calendar_Object.set(Calendar.HOUR_OF_DAY, 16);
    Calendar_Object.set(Calendar.MINUTE, 51);
    Calendar_Object.set(Calendar.SECOND, 0);

    // MyView is my current Activity, and AlarmReceiver is the
    // BoradCastReceiver
    Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
            0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    /*
     * The following sets the Alarm in the specific time by getting the long
     * value of the alarm date time which is in calendar object by calling
     * the getTimeInMillis(). Since Alarm supports only long value , we're
     * using this method.
     */

      Long alarmTime = Calendar_Object.getTimeInMillis();

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7 * 24 * 60 * 60 * 1000 , pendingIntent);
Deepanshu Gandhi
  • 490
  • 3
  • 10