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?