Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - What happens to a bound service if the binding activity was killed?

Android provides the Service class, which can be useful for background or non-UI operations.

I have a question about Services' lifecycle.

I know that bound services have the lifecycle like following:

  1. Some component starts the Service via bindService() -> onCreate()
  2. onBind()
  3. process
  4. The binding component calls unbindService() -> onUnbind()
  5. onDestroy()

My question is:

Activities usually call unbindService() at onStop(). However, the Activity can be killed without calling onStop() - I mean, when the system memory is low, the only method that must be called is onPause(). onStop() is after onPause(). Before calling onStop(), the Activity can be destroyed.

In this case, the Service didn't get unbindService(), so the Service is still running. Is this right?

Of course, this rarely happens because Services are background by default. (Services are more likely to be killed by system on low memory.) However, a "Foreground" Service has higher priority than the "onPause()ed activity." according to http://developer.android.com/guide/components/processes-and-threads.html . In this case, the binding activity will be killed first.

If this thing happens, the Service does not end? If memory is not low anymore, then the Activity will be created again, but will call bindService() again since it is a new instance. Also, the Activity even may not restart. Isn't this right? What can I do in this case?

like image 954
Naetmul Avatar asked May 22 '13 15:05

Naetmul


People also ask

How does bound service work in Android?

It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

What is the difference between started service and bound service?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.

What is unbound service in Android?

Unbound Service or StartedA 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.

What is Android bond service?

A bound service is like a server in a client-server interface. A bound server allows components, such as activities, to bind to the service, send requests, receive responses and even perform IPC or Inter-Process Communication.


1 Answers

The Service is killed, but if you have 'return START_STICKY' being returned from the onStartCommand(...) [AND you are starting the service using 'startService(intent)'], the service will start back up again. The Service will start back up even if the Activity is not opened again.

I have run this example - the BoundedAudioService example and tested by killing the activity - the service restarts itself. (By restart I mean, the onStartCommand(...) of the service is called again)

like image 67
user1406716 Avatar answered Sep 29 '22 01:09

user1406716