I have three alarm managers to create three alarms at different of the day. These alarms are in the oncreate method of the mainactivity. I want these alarms to be set only when the application starts the first time. But because they are in the oncreate method of the main activity. They are recreated everytime my main activity is recreated. How can I fix this problem? Also I want the alarms to be set off even if my main activity is not active or in focus.
Moreover, I want each of these alarms to repeat daily at different times within a certain time window. The setRepeatingAlarm method does not give me that flexibility. Any ideas on how I can accomplish this?
Intent intent1 = new Intent(this, ReminderAlarmReceiver.class);
intent1.putExtra("alarm_message", "Alarm!");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent1, PendingIntent.FLAG_NO_CREATE);
Intent intent2 = new Intent(this, ReminderAlarmReceiver.class);
intent2.putExtra("alarm_message", "Alarm!");
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 2, intent2, PendingIntent.FLAG_NO_CREATE);
Intent intent3 = new Intent(this, EODAlarmReceiver.class);
intent3.putExtra("alarm_message", "Alarm!");
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 3, intent3, 0);
// Get the AlarmManager service
AlarmManager alarmMgr1 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr1.set(AlarmManager.RTC_WAKEUP, cal0.getTimeInMillis(), pendingIntent);
AlarmManager alarmMgr2 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr2.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pendingIntent2);
AlarmManager alarmMgr3 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr3.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), pendingIntent3);