Background Service Getting Killed in Android

Background Service getting killed in android

The main problem is that we cannot say

Services are not meant to be killed. They are meant to run in background as long as we want it to.

Basically, that is not true. System still can terminate the service in low memory and possibly other situations.
There are 2 ways to overcome this:

  1. If you are implementing the service, override onStartCommand() and return START_STICKY as the result. It will tell the system that even if it will want to kill your service due to low memory, it should re-create it as soon as memory will be back to normal.
  2. If you are not sure 1st approach will work - you'll have to use AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html . That is a system service, which will execute actions when you'll tell, for example periodically. That will ensure that if your service will be terminated, or even the whole process will die(for example with force close) - it will be 100% restarted by AlarmManager.

Chinese Android Devices Killing Background Service

This will happen mostly for optimizing battery and improving phone performance,

As you have tried my solution but still you are getting this issue in some devices,
so here is an alternate solution for killing app on swipe from recent app list,

  1. Ask User to Swipe down the application which will lock application as white list and wont be killed

OR


  1. Add below line for every activity in manifest, which will hide your app from recent app list. hence user cant swipe app from recent apps list.

    android:excludeFromRecents="true"

App in background killed after 5 minutes in Android 9

you need to have an foreground service, otherwise the OS will kill you app



Related Topics



Leave a reply



Submit