0

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

Joseph. S
  • 1
  • 2

1 Answers1

0

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 .

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • 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 – Joseph. S Aug 07 '20 at 12:22
  • Check https://stackoverflow.com/questions/30525784/android-keep-service-running-when-app-is-killed, maybe helpful. – ColeX Aug 10 '20 at 08:39