Note: Somewhere is StackOverflow have some answers to this question. but which is not helped me and the answers are not correct in my case. here are the links to those questions.
- AlarmManager: how to schedule a daily alarm and deal with time changes
- Android AlarmManager and DST/Timezone/Time change?
I am encountered one issue with Android's alarm manager. I am scheduling a repeating notification on DST enabled area. Here are my timezone settings
- Region: Canada
- TimeZone: Vancouver
- Device: S9+ Android 10
- DST Starts on - March 8 & DST ends on - Nov-1
I scheduled a notification on February 29 at 9:00 AM notification comes successfully, but after DST Starts on March 9th the notification fired at 10:00 AM. after DST ends it is back to normal. I need to fire the notification at exactly 9:00 AM every day independent of DST. Is this behavior is normal? there are no issues with iOS devices.
- Is this is a bug from the Android side?
- How to schedule the notification independent of DST?
Can anyone help me?
Here is my code to schedule the notification
Note: I have added the AlarmManager.RTC_WAKEUP as the alarm type and scheduled the interval as AlarmManager.INTERVAL_DAY. also, passed the UTC time to the alarm manager from the calendars calendar.getTimeInMillis() method.
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
Test Procedure:
Setting the Region: Canada & TimeZone: Vancouver
- Schedule a repeating notification on Feb 15th at 9:00 AM
- Test whether the notification fired on Feb 16th at 9:00 AM -> Success, notification fired at 9:00 AM
- change the system time to March 9th (Which is on DST); observing the notification fires at 9:00 AM -> Failed, it fired on 10:00 AM
- Change the date to Nov 2nd (DST Ended) ; observing the notification fires at 9:00AM -> Success notification fired at 9:00 AM