AlarmManager object after turning the phone off and on

In my application, I set an alarm

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); ... PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); ... alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent); 

It works great if I do not turn off and turn on the phone.

To be more specific, let's say at 10:20, I set the alarm at 10:22, and I turned off and turned on the phone at 10:21, the alarm will not work.

What could be the problem? Is this a broadcast problem for the pending object, or do I need to set some flags of the alarmManager object to work in such conditions?

+6
source share
1 answer

The AlarmManager documentation says that:

The registered alarms are saved when the device is sleeping (and if necessary can wake the device if they go out during this time), but it will be cleared if it is turned off and rebooted .

AlarmClock, turned on by default by Android, seems to work even after a reboot.

In the event that your alarms work after a reboot, you need to start the application at boot and configure all alams again using AlarmManager. (In fact, you can simply configure your alarms via broadcast rather than launching the application).

Here is the StackOverflow question regarding app lunch at startup.

You will also check how, by default, AlarmClock does this by reading from the source. You can read and download it from here.

+17
source

Source: https://habr.com/ru/post/905424/


All Articles