I want to set an alarm to check the server for getting new data everyday at 7 AM , 12 AM and 10 PM.
I tried this way :
public void startAlarm(Context context) {
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ShowNotificationReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar firstTurn = Calendar.getInstance();
Calendar secondTurn = Calendar.getInstance();
Calendar thirdTurn = Calendar.getInstance();
// set times
firstTurn.set(Calendar.HOUR_OF_DAY,FIRST_TURN_HOUR);
firstTurn.set(Calendar.MINUTE,FIRST_TURN_MINUTE);
secondTurn.set(Calendar.HOUR_OF_DAY,SECOND_TURN_HOUR);
secondTurn.set(Calendar.MINUTE,SECOND_TURN_MINUTE);
thirdTurn.set(Calendar.HOUR_OF_DAY,THIRD_TURN_HOUR);
thirdTurn.set(Calendar.MINUTE,THIRD_TURN_MINUTE);
alarmMgr.cancel(alarmIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, secondTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, thirdTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
If i comment 2 last line its work correctly and fire in time , but it doesnt work if i want all of them .
Where is my mistake and how can i fix this ?
After some search and playing more with code i resolve my problem in this way :
public void startAlarm(Context context) {
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ShowNotificationReceiver.class);
Calendar firstTurn = Calendar.getInstance();
Calendar secondTurn = Calendar.getInstance();
Calendar thirdTurn = Calendar.getInstance();
// set times
firstTurn.set(Calendar.HOUR_OF_DAY, FIRST_TURN_HOUR);
firstTurn.set(Calendar.MINUTE, FIRST_TURN_MINUTE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
secondTurn.set(Calendar.HOUR_OF_DAY, SECOND_TURN_HOUR);
secondTurn.set(Calendar.MINUTE, SECOND_TURN_MINUTE);
PendingIntent alarmIntent2 = PendingIntent.getBroadcast(context, REQUEST_CODE2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
thirdTurn.set(Calendar.HOUR_OF_DAY, THIRD_TURN_HOUR);
thirdTurn.set(Calendar.MINUTE, THIRD_TURN_MINUTE);
PendingIntent alarmIntent3 = PendingIntent.getBroadcast(context, REQUEST_CODE3, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.cancel(alarmIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, secondTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent2);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, thirdTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent3);
}
As explained in this answer
If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntents with different requestCode. If requestCode is the same, then the new alarm will overwrite the old one.