Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a started and bound Service destroyed?

I was going through the services documentation in android when I noticed two contradicting points:

In the services document it is specified in Managing the Lifecycle of a Service

These two paths are not entirely separate. That is, you can bind to a service that was already started with startService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.

But in the document about bound services in Managing the Lifecycle of a Bound Service

However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service, because the service is now considered to be started. In this case, the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients.

It may be me but I think the statements are contradictory.Could anyone please clarify...

like image 837
anz Avatar asked Jun 17 '13 11:06

anz


People also ask

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 the difference between bound and unbound service in Android?

The Unbound service runs in the background indefinitely. Where As The Bound service does not run in the background indefinitely. The Unbound service is stopped by stopService() method. Where As In The Bound service, The client can unbind the service by calling the unbindService() method.

What is the life cycle of services in Android?

Lifecycle of Android Services. Android services life-cycle can have two forms of services and they follow two paths, that are: Started Service. Bounded Service.


1 Answers

Agree that the documentation could be clearer. What they are trying to say is:

  • If you call startService(), then the service will keep running unless and until you call stopSerivce() (or stopSelf() from within the service)
  • If you call bindService(), then the service will keep running unless and until you call unbindService()
  • Therefore, if you call both startService() and bindService(), then the service will keep running until you call both stopService and unbindService(). Neither on its own will stop the service.

Created a very simple Activity and Service and ran the following sequences of start/stop/bind/unbind. I observed that the calls gave the following results.

bind-unbind

bindService() caused:     onCreate()     onBind() unbindService() caused:     onUnbind()     onDestroy() 

start-bind-unbind-stop

startService() caused:     onCreate()     onStartCommand() bindService() caused:     onBind() unbindService() caused:     onUnbind() stopService() caused:     onDestroy() 

start-bind-stop-unbind

startService() caused:     onCreate()     onStartCommand() bindService() caused:     onBind() stopService() caused:     -- nothing unbindService() caused:     onUnbind()     onDestroy() 

bind-start-stop-unbind

bindService() caused:     onCreate()     onBind() startService() caused:     onStartCommand() stopService() caused:     -- nothing -- still running unbindService() caused:     onUnbind()     onDestroy() 

bind-start-unbind-stop

bindService() caused:     onCreate()     onBind() startService() caused:     onStartCommand() unbindService() caused:     onUnbind() stopService() caused:     onDestroy() 

As you can see, in each case where both bind and start were called, the service kept running until both unbind and stop were called. The sequence of unbind/stop is not important.

Here is the example code that was called from separate buttons in my simple test app:

public void onBindBtnClick(View view) {     Intent intent = new Intent(MainActivity.this, ExampleService.class);     bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); }  public void onUnbindBtnClick(View view) {     if (serviceIsBound) {         unbindService(serviceConnection);         serviceIsBound = false;     } }  public void onStartBtnClick(View view) {     Intent intent = new Intent(MainActivity.this, ExampleService.class);     startService(intent); }  public void onStopBtnClick(View view) {     Intent intent = new Intent(MainActivity.this, ExampleService.class);     exampleService.stopService(intent); } 
like image 191
Chuck Krutsinger Avatar answered Sep 27 '22 21:09

Chuck Krutsinger