Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am a bit confused about how services function, notably these two things:

From http://developer.android.com/guide/topics/fundamentals/services.html :

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.

and:

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).

My questions:

  1. How is it possible for a service that was started by startService() survive, if the main Activity thread quits? Or only those services survive the main Activity shutdown, that are in a separate thread?

  2. How can I start a service in a separate thread? The dev doc at http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingService only shows an example how to spawn a thread when the service is created...not how to spawn the service itself in a new thread...
    EDIT: Is android:process= in manifest.xml used for starting it in a separate thread? (if it starts it in a new process, then it must be in a separate thread..)

like image 981
sydd Avatar asked Dec 02 '11 17:12

sydd


2 Answers

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.

like image 180
Kiran Kuppa Avatar answered Oct 16 '22 13:10

Kiran Kuppa


  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.

like image 28
Ted Hopp Avatar answered Oct 16 '22 13:10

Ted Hopp