1

I have created an intent service that downloads some files, writes some of them to external storage and some data to my app's local database

It was working fine until I realised that the intent service runs in the same process of my app.

I want the service to do its job even if the app process terminates. I noticed that if I terminate my process, the intent service dies too and does not do the task.

So I read that I can run my intent service in a separate process which I done here

 <service
            android:name=".services.DownloadContentIntentService"
            android:process=":whatever"
            android:exported="false" />

Now the task runs in a separate process. It still runs if I terminate my app which is working.

However, I noticed that the intent service does not terminate itself once the task is done.

Even when I call stopSelf(). According to Android Studio, it is still alive.

What is happening?

Thank you for reading.

Ersen Osman
  • 7,067
  • 8
  • 47
  • 80

1 Answers1

3

Android is essentially Linux based, so processes should be viewed always in that context. Now, the Service is provided by Android, but the sandbox is provided by Linux. Moreover, things get more complicated, because there is JAVA in picture as well, and a Virtual machine essentuates things more. stopSelf() by a service does in no way mean that the process containing the service is killed. To kill a process, you need to execute SIGKILL aka kill -9

So, even when Android has been signaled for stopSelf() by the service, Linux may still decide to keep the process alive, especially because before terminating a process, the resources need to be GCed. Thus, stopSelf() is in no manner equivalent to SIGKILL, which would essentially mean that you are not able to see the service in PS list (or Android's process list) immediately

Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39