Solution 1 :
AlarmManager work perfectly when the App is running in Foreground or in Background.
But when the App is quit/force-stopped/killed , Alarm Manager is cancelled.
Alarm will go off again only the App is again launched by the user.
To keep alarm alive when app closed you might have to use Foreground service .
Here is a sample you could refer to .
Problem :
I have an alarm that fires every hour and executes code in specific conditions
It works smoothly, but when I leave my app unopened (the alarm initialization is on app startup) it stops after some time, these are the logs:
8/2/2020 6:27 AM Initializing Alarm, next alarm will fire at 8/2/2020 3:27 AM
8/2/2020 6:27 AM Application starting.
8/2/2020 6:28 AM Application stopped.
8/2/2020 6:28 AM Reports alarm received.
8/2/2020 7:28 AM Reports alarm received.
8/2/2020 8:28 AM Reports alarm received.
8/2/2020 9:28 AM Reports alarm received.
8/2/2020 9:28 AM Reports alarm finished.
8/2/2020 10:28 AM Reports alarm received.
8/2/2020 10:28 AM Reports alarm finished.
8/2/2020 11:28 AM Reports alarm received.
8/2/2020 11:28 AM Reports alarm finished.
8/2/2020 12:30 PM Reports alarm received.
8/2/2020 12:30 PM Reports alarm finished.
8/2/2020 1:30 PM Reports alarm received.
8/2/2020 1:30 PM Reports alarm finished.
8/2/2020 2:30 PM Reports alarm received.
8/2/2020 2:30 PM Reports alarm finished.
8/3/2020 8:13 AM Initializing Alarm, next alarm will fire at 8/3/2020 5:13 AM
As you see between 2:30 PM to 8:13 AM the next day, no alarms were fired
This is the initialization:
alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 60 * 60, alarmIntent);
I have acquired the WAKE_LOCK permission.
Thanks in advance
Comments
Comment posted by Joseph. S
I’m not so sure that’s how AlarmManager works, because I see many apps which doesn’t use any services and still get to run code periodically, For fact: when my app is killed – it still works for a few hours then stops
Comment posted by stackoverflow.com/questions/30525784/…
Check