3

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 ?

matdev
  • 4,115
  • 6
  • 35
  • 56
  • 1
    No actually you cannot change behaviour of os however you can always create a sticky service to keep performing your task even if app gets closed also sometimes service is also automatically killed by os so to solve that use self start permission with foreground service – Mr. Patel Nov 07 '19 at 10:51
  • 1
    This might be helpful https://dontkillmyapp.com/ – Ivan Wooll Nov 07 '19 at 11:01
  • 1
    I'll leave this here as it's always an interesting read: https://dontkillmyapp.com/ – Nikos Hidalgo Nov 07 '19 at 11:01
  • Very interesting read, especially about Huawei's PowerGenie which apparently kills apps that are not on its whitelist – matdev Nov 07 '19 at 13:47

1 Answers1

1

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

kostyabakay
  • 1,649
  • 2
  • 21
  • 32
  • 3
    you can't "avoid its killing". At best you can lower the odds of it happening – Tim Nov 07 '19 at 11:08
  • I accept your answer... to be complete, it seems using a permanent notification and setting its importance to NotificationManager.IMPORTANCE_HIGH also contribute to lower the odds of the app being killed. – matdev Nov 18 '19 at 09:12
  • @matdev did you manage to fully avoid getting your app killed? Is there any more settings that helped besides setting NotificationManager.IMPORTANCE_HIGH? – Cilvet Jun 12 '20 at 22:15
  • you cannot fully avoid getting your app in background killed if the OS decides to, you can only lower the odd of your app being killed. Have you considered using a JobIntentService instead of a foreground service ? – matdev Jun 15 '20 at 12:25
  • I have this problem too (testing on a Huawei P Smart), but implementing as a foreground service is not sufficient. For instance, the Samsung Health app does work as a foreground service, but it still get killed by the P Smart. I found no other way than manually going through the usual path Settings > Battery > App Launch > Manage Manually) – Franco Jul 02 '20 at 21:55
  • How this answer can mark as accepted? Indeed app like Spotify use a foreground service but this is not an acceptable solution to create a fake foreground service if your app do not need it just to prevent being killed (although it's really annoying!). This is just a bad trick here. – Cyril Oct 26 '21 at 09:45