Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote service killed while main process bound to it

On my Galaxy Nexus with Jelly Bean 4.1.1 (official) sometimes the OS goes on a killing spree and the log has these kinds of entries:

10-02 22:24:34.992 I/ActivityManager(  306): Killing 7517:com.google.android.apps.reader/u0a77: remove task
10-02 22:24:36.484 I/ActivityManager(  306): Killing 7465:com.tf.thinkdroid.sg:writedroid/u0a50: remove task
10-02 22:24:37.273 I/ActivityManager(  306): Killing 5924:com.metago.astro/u0a73: remove task
10-02 22:24:37.296 W/ActivityManager(  306): Scheduling restart of crashed service com.metago.astro/.jobs.JobService in 5000ms
10-02 22:24:37.656 I/ActivityManager(  306): Killing 7302:org.jtb.alogcat/u0a84: remove task
10-02 22:24:38.148 I/ActivityManager(  306): Killing 7120:com.google.android.gm/u0a19: remove task

Unfortunately one of the processes being killed is my remote service that has a running app bound to it. Indeed, the service is restarted immediately afterwards but this causes inconsistent behavior in my app.

In Service Lifecycle it says:

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag.

My app binds to the remote service with Context.BIND_AUTO_CREATE so I was under the impression that the remote service will stay up as long as the main process is bound to it. How can I make Android not kill the remote service?

NOTE: The bounty is for explaining why the code doesn't seem to work according to the documentation.

like image 280
gnobal Avatar asked Oct 04 '12 08:10

gnobal


1 Answers

Please, read this answer from Joel F here: How it is possible Service run indefinitely and also allow binding in android?

As mentioned, if there is really a need for resources, the only way of keeping your service unkillable is to use foreground service http://developer.android.com/guide/components/services.html#Foreground

The documentation is also written wrongly because the citation : "system will keep the service running as long as either it is started or there are one or more connections to it" is not true in 100% of cases. The Context.BIND_AUTO_CREATE flag will give a higher privilege to your service but not hight enough to keep it unkillable. The other services will be killed first and if there is still need for ressourcees, your privileged service will be also killed.

like image 177
Milos Cuculovic Avatar answered Nov 03 '22 18:11

Milos Cuculovic