问题
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
回答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 .
来源:https://stackoverflow.com/questions/63288837/android-alarm-stops-firing-after-some-time