Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Oreo killing background services and clears pending alarms, scheduled jobs after entering doze mode

My app has a background service running that gets users current location and update it to a server every five minutes. To run this location update process continuously, I use alarm manager to set its next execution time from the service itself. However, when I install the app in my Nokia 6 running Android 8.1 it works for some time and if I keep the phone idle for some time, my service will get killed with the next alarms by the application also being cleared from system alarm manager. My guess was that the idle time makes the phone enter doze mode. However, I don't understand why the alarm managers got cleared. To my understanding, the doze mode should open up maintenance windows periodically to execute any pending tasks.

To mitigate this issue, I tried to apply a JobScheduler service on top of AlarmManager, which runs every 15 minutes. Purpose of this jobscheduler was to re-start the service which has the alarmmanager in it, so even if it gets killed and the alarm is cleared, jobscheduler would re-up the service.

After I tested this patch and keeping it for some time to go into idle mode, it resulted in getting both JobScheduler Service and Service which has the alarm in it killed with the scheduled jobs and alarms getting cleared from the system.

It is said in the Android documentation that we can use JobScheduler to mitigate its background execution limitations. And to test this out I forced killed the two services when I tested the app, but the already scheduled job did not get cleared, and it made the service with the alarm run again successfully. I don't understand the reason for this behavior, although the Evernote guys give an explanation that could match this scenario in here Android Job by Evernote

Any ideas for this abnormal behavior?

Test Environment Details

  • Device : Nokia 6 (TA-1021)
  • OS : Android 8.1.0
like image 944
k9yosh Avatar asked May 24 '18 08:05

k9yosh


2 Answers

You would not be able to run background services long running in Oreo as there are behaviour changes, now Oreo to optimise system memory, battery etc, it kills background service, to solve your issue you should use foreground service.

Have a look at Background execution limits https://developer.android.com/about/versions/oreo/android-8.0-changes

A suggestion from me, if you can use FCM then go for it, becasue apps like WeChat, Facebook uses it, to deliver notifications and they don't face any problem...

Hope this helps in understanding the issue....

like image 191
Abdul Aziz Avatar answered Nov 01 '22 08:11

Abdul Aziz


In Doze more, the alarms do not get reset, but get deferred to a later time. You have two mainstream options here:

  1. Use either setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). However, these too can fire at the maximum frequency of 1 time per 9 minutes. So you'll have to decrease the frequency at which you get location in your app.

  2. Use a foreground service by way of showing a foreground notification. Everyone does that (apps like Uber, Google Maps etc). That way, your service won't get killed, and be treated as though you have an app open.

like image 1
Abdul Wasae Avatar answered Nov 01 '22 08:11

Abdul Wasae