Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why service is still running if its process is killed?

I've checked, through decompilation, that in the manifest of some applications a service is not started with a separate process. By the way, in the task manager, it appears as "running".

For example Facebook, on Android 4.4.2, has got 0 processes and 2 services running. From the documentation (and correct me if I'm wrong) I've understood that a Service runs in the same process of its Application. If the process dies, the service is stopped (and can be restarted automatically by an AlarmManager or START_STICKY).

So, if it's not started inside a separate process, there should be at least one process where the service can run. So, is it possible that a service runs even if there are no processes? I repeat, the service (from manifest) HASN'T GOT the directive android: background

EDIT 1: It seems that it is a bug of Android 4.4.2. See this and this. In my case I encounter the problem when I swipe the app from the list (as described in second link)

EDIT 2: Please correct me if I'm wrong, this is what I've understood.

A Service object is created for the first time when startService() is called. onCreate() will be called on that Service object and onStartCommand() will follow. From now on:

  • If the process dies, the service object is still there (if stopSelf() is not called) but it isn't running.
  • If the service dies, the only way to be recreated is from START_STICKY (and other restart constants) or manual call to startService(). When the service is killed from the system, onCreate will be called again.

Now, this is the normal behavior on Android < 4.4.2. On KitKat, if you swipe the application from the LRU list, the process dies and even with START_STICKY it is not automatically restarted. Workaround is an AlarmManager. Is it correct?

like image 874
Angelo Avatar asked Nov 11 '22 11:11

Angelo


1 Answers

Your Service is part of your application. By definition, your application is running when the Service is running. When, for instance, the AlarmManager invokes your Service at a scheduled time, Android first starts a process for the application (if it is not already running), and then invokes your Service within the now running app.

In general, you are correct: If the application (process) dies, the Service will die with it.

Android supports a Manifest attribute, android:process, which allows you to specify that a particular component of your application should be run in a separate process. This, essentially, runs multiple, identical, copies of your application, in multiple processes. Android simply uses one of the processes to run one component and another to run the other. Your application is running in both.

The source of confusion is the way Settings > Apps page uses the words "process" and "service". It appears to use the former to count processes with UI components and the latter to count all other processes. If you use adb to connect to your device and use the "ps" command you'll get something that much better reflects the common understanding of those words.

like image 127
G. Blake Meike Avatar answered Nov 14 '22 21:11

G. Blake Meike