Foreground service getting killed from Oreo

I've analyzed the issue on OnePlus, as I'm in the same situation as you. As I see currently, there is no solution. OnePlus clearly follows a bad practice.

As they haven't released the source code which kills processes in this manner, I downloaded a OnePlus ROM (I choose OnePlus 5T 5.1.5), extract it, find the .class which does this (OnePlusHighPowerDetector.class in services.vdex), decompile it, and tried to find out what's going on.

You can find a version of this class here (it is not by me, and maybe it is not the same version that I've used): https://github.com/joshuous/oneplus_blobs_decompiled/blob/master/com/android/server/am/OnePlusHighPowerDetector.java

Unfortunately, the most important functions aren't decompiled successfully. But we can analyze the bytecode anyway. Here's what I've found:

  • OnePlus kills background processes (it seems that it doesn't matter whether it has a foreground service or not) without mercy. Almost all of them. It doesn't really matter, how little CPU resources are used. My app uses less than 1%, and it gets killed.
  • If the app is pinned in the recent app list (determined by a list of com_oneplus_systemui_recent_task_lockd_list), it doesn't get killed. So, the user can save the app by pinning it. But it is inconvenient for them.
  • Oneplus provides a list of processes, which they don't kill. This list is in oneplus-framework-res.apk/res/values/array.xml, with the key string-array name="backgroundprocess_detection_app_whitelist". This list mostly contains map and fitness applications1.
  • Maybe there are other factors which can save a process being killed, I haven't analyzed the issue further. If you have the time, check out OnePlusHighPowerDetector.java.

Here are the steps needed to decompile a .vdex file:

  • use Vdex Extractor to create a .dex from .vdex (maybe you'll need to use the --ignore-crc-error option)
  • use JADX to decompile a .dex (this is currently the best decompiler in my opinion), or use dex2jar to create a .jar from .dex, and use any decompiler to decompile the .jar

1Rant: this shows how bad the current situation is. OnePlus, is this really the solution? You've chosen 10-15 applications, which can run in the background, and you don't care about all the other ones? How can one create a new fitness/map/music player application, which works safely on your device?

I spent several hours solving this issue. Xiaomi phone, Oreo. I have solution, 2 steps:

  1. Xiaomi needs additional permission: Settings->Permissions->Automatic startup. There is a list of applications that are allowed to be started automatically. BOOT_COMPLETED and START_STICKY is NOT enough. Facebook, Outlook, Skype + my app is listed here. I do not know how to ask user programatically to grant this permission.

  2. Service gets recreated but NOT restarted, even if you return START_STICKY. After you kill the app (swipe out from running apps), onCreate is called again but NOT onStartCommand. Try Log.d() to see it. I moved all the logic into onCreate EXCEPT creating the notification, calling startForeground() and returning START_STICKY. This has to remain in onStartCommand().

Batery optimization must be disabled for the application too, but it was required by previous version of Android already.

Now my service keeps running or is recreated with 100% success within few seconds.

use startForeground(id, notification) method inside your service.

User should know that something, that cant be killed by system, is working in background, so android system should inform user with a notification, so user knows exactly what is killing his battery.

By the way you can use WakeLock.

 PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PARTIAL_WAKE_LOCK, 
 TAG_FOR_DEBUG);
 wakeLock.acquire()

But read about WakeLock before using it. And don't forget to disable it when your work is finished.

You can check current enabled wake locks using adb shell dumpsys power command in the Terminal

I faced the same issue in my OnePlus3 for my app. Then I noticed these lines in logcat, every time my app and Foreground service were killed:

10-12 18:30:40.644  1439  1519 I OHPD    : [BgDetect]force stop com.mycompany.MyAndroidApp (uid 10905) level 0
10-12 18:30:40.645  1439  1519 I ActivityManager: Force stopping com.mycompany.MyAndroidApp appid=10905 user=0: from pid 1439
10-12 18:30:40.645  1439  1519 I ActivityManager: Killing 23139:com.mycompany.MyAndroidApp/u0a905 (adj 200): stop com.mycompany.MyAndroidApp
10-12 18:30:40.647  1439  1519 W ActivityManager: Scheduling restart of crashed service com.mycompany.MyAndroidApp/md55852635cfc9dced970aa39ae09e74ead.MyForegroundService in 1000ms
10-12 18:30:40.651  1439  1519 I ActivityManager:   Force finishing activity ActivityRecord{a5c8d39 u0 com.mycompany.MyAndroidApp/md55852635cfc9dced970aa39ae09e74ead.MainActivity t3108}
10-12 18:30:40.655  1439  1519 I ActivityManager:   Force stopping service ServiceRecord{6052f94 u0 com.mycompany.MyAndroidApp/md55852635cfc9dced970aa39ae09e74ead.MyForegroundService}
10-12 18:30:40.660  1439  1519 I OHPD    : [BgDetect]chkExcessCpu level: 1 doKills: true critical false uptime: 300328
10-12 18:30:40.710  1439  8213 I WindowManager: WIN DEATH: Window{c381342 u0 com.mycompany.MyAndroidApp/md55852635cfc9dced970aa39ae09e74ead.MainActivity}

Then searching the internet for the keywords 'OHPD : [BgDetect]chkExcessCpu level: 1 doKills: true' took me to this SO answer:

Prevent background service from being killed due to "detect excessive cpu on forked process"

As suggested in that answer and the GitHub issue, Locking / Pinning my app prevented Oxygen OS from killing my app (MainActivity and Forground Service).

in oneplus 6t, run this adb command

setting put system com_oneplus_systemui_recent_task_lockd_list {com.topjohnwu.magisk/a.c#0}{com.mycompany.MyAndroidApp/com.mycompany.MyAndroidApp.MainActivity#0}

prevent killing com.topjohnwu.magisk and com.mycompany.MyAndroidApp