How to prevent app being killed in background?

4k Views Asked by At

On my Huawei P20 Lite (Android 9), there is a setting Power usage details -> app launch -> "Manage automatically". With this setting enabled, I noticed that the OS sometimes kills my app when it is in background.

enter image description here

The app acts as a streaming server.

Is it possible to programmatically disable "Manage automatically" and enable the "Run in background" settings ?

EDIT: I noticed that famous app such as Deezer or Spotify don't request ignoring battery optimization and they are never killed when playing music in background. Does anyone know how they achieve that ?

1

There are 1 best solutions below

6
kostyabakay On BEST ANSWER

Implement Foreground Service in your app to increase the component priority in OS to lower the odds of its killing.