I am working on an android app for college work and I am trying to set a repeating alarms for specific days (or everyday. both options are possible). Here is my code for this:
public void createAlarms(List<Reminders> medWreminders) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
for (int i = 0; i < medWreminders.size(); i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar setcalendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, medWreminders.get(i).reminder.getHour());
calendar.set(Calendar.MINUTE, medWreminders.get(i).reminder.getMinutes());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_WEEK, medWreminders.get(i).reminder.getDayInt());
// cancel already scheduled reminders
notificationHelper.cancelNotification(this, medWreminders.get(i).reminder.getId());
if (calendar.before(setcalendar)) {
calendar.add(Calendar.DATE, 1);
}
// enable receiver
ComponentName receiver = new ComponentName(this,
AlertReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
medWreminders.get(i).reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
It's important to mention that if the alarm is for daily basis, instead of setting one repeating alarm on a daily interval, (for other reasons) I'm setting 7 different alarms based on weekly interval- one for each day (which, I think, should behave the same). And remember, it is also for specific days in week. So the problem is: if I set alarms- one for each day- at the same day it works but on the next day nothing happens. Does anyone know the problem? Thanks!