Android - How is it possible, that a service keeps running after the Activity has been shut down? Android - How is it possible, that a service keeps running after the Activity has been shut down? multithreading multithreading

Android - How is it possible, that a service keeps running after the Activity has been shut down?


To quote you,:

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise).

When the main activity is shutdown, it does not necessarily mean the hosting process exits. According to the documentation, the service continues to run because the process is alive!

Check this

The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher

So on only when running low on resources and it is required to kill processes lying around, your process would be killed. Else. the process lives on so would your service.


  1. A started service can survive the shutdown of an activity that starts it in exactly the same way that one activity can survive the shutdown of another activity that starts it with startActivity(). Activities and services are just two separate components of your application process. Once started, each exists independently of the other. (Things are a bit different with bound services — the system will shut down a bound service when nothing is bound to it anymore.)

  2. You cannot start a service in a separate thread, any more than you can start an activity in a non-event thread. You can only ask the system to start the service (via startService() or bindToService()). The system always starts the service on the event thread.