Android: Keeping a Background Service Alive (Preventing Process Death)

Android: keeping a background service alive (preventing process death)

For Android 2.0 or later you can use the startForeground() method to start your Service in the foreground.

The documentation says the following:

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

The is primarily intended for when killing the service would be disruptive to the user, e.g. killing a music player service would stop music playing.

You'll need to supply a Notification to the method which is displayed in the Notifications Bar in the Ongoing section.

When Service is killed, can the process be still alive?

I'm 99,9% sure of this: if the service goes, there goes the process too. The conditions to kill a service are pretty demanding such as in low memory situations. My own experience is that the whole app is killed when the service dies. The service and the process are tied together. Yes, you can manually stop your service without killing your process, but I'm almost certain that when the OS kills your service because of low memory, then bye-bye process.

See Hackbod's answer and comments: Android service killed

See http://developer.android.com/reference/android/app/Service.html and onLowMemory:

This is called when the overall system is running low on memory, and
would like actively running process to try to tighten their belt.
While the exact point at which this will be called is not defined,
generally it will happen around the time all background process have
been killed, that is before reaching the point of killing processes
hosting service and foreground UI that we would like to avoid killing.

Background info: http://about-android.blogspot.com/2010/07/lifecycle-of-android-application.html

This is also interesting: Will we leak the android service connection if the client's process is killed by android?

keeping background service alive after user exit app

According to the Android documentation you can achieve this behavior by using the attribute:

 android:isolatedProcess="true"

By the way, I know that it's not answering the question but it might help some people as well - lately, I found out about a great third-party lib that Evernote developers have created. Its name is Android-Job and its aim is to create jobs that run on different processes and can become active again even after a device reboot, it's amazing.

Keeping a background service alive on Android

You need to use a Partial Wakelock with the service. If your service is interactive with the user, you might consider making the service foreground.

This is a working example for a Partial Wakelock:

Use this when service is started:

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
cpuWakeLock.acquire();

When the service is stopped, do call cpuWakeLock.release();

imports android.support.v4.content.WakefulBroadcastReceiver which
Android Studio is telling me doesn't exist.

You need to import the support library for that.

Android: Service is killed and restarted after a while

If you are using a background Service with a scheduled task, it could be killed by the system. The only way to prevent the killing is a foreground Service. Quoting the documentation:

A foreground service is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory.

You have to call the method startForeground() inside your Service using a Notification to show it. For further information you can check: https://developer.android.com/guide/components/services.html#Foreground

By the way, I recommend you to use the new JobScheduler api above api 21.
https://developer.android.com/reference/android/app/job/JobScheduler.html



Related Topics



Leave a reply



Submit