android service startService() and bindService()

Android

Android Problem Overview


I would like to know if it is possible to have a service that is started with startService and then be able to also bind to that service and do some remote procedure calls? according to this:http://developer.android.com/guide/topics/fundamentals.html#servlife

the two services have different lifecycle so it's not possible,does anyone know about it?

Android Solutions


Solution 1 - Android

I think hara's answer was a little confusing. What you describe is perfectly legitimate and in fact the only way to get the behavior you want. If you create a Service by binding to it, it will die when you unbind. So the only way to keep it around without activities binding to it is to start it with startService(). There is no conflict with lifecycles because it only applies to how the service is STARTED. So once it's started with startService(), it follows that lifecycle process. So you are free to bind and unbind to it as much as you wish and it will only die when you call stopService() or stopSelf().

Solution 2 - Android

Yes, You can start and bind (one or more times) the same service.

The following flowchart demonstrates how the lifecycle of a service is managed. The variable counter tracks the number of bound clients: enter image description here

Good example - music app. Explanation from Building a Media Browser Service official tutorial:

> A service that is only bound (and not started) is destroyed when all > of its clients unbind. If your UI activity disconnects at this point, > the service is destroyed. This isn't a problem if you haven't played > any music yet. However, when playback starts, the user probably > expects to continue listening even after switching apps. You don't > want to destroy the player when you unbind the UI to work with another > app. > > For this reason, you need to be sure that the service is started when > it begins to play by calling startService(). A started service must be > explicitly stopped, whether or not it's bound. This ensures that your > player continues to perform even if the controlling UI activity > unbinds. > > To stop a started service, call Context.stopService() or stopSelf(). > The system stops and destroys the service as soon as possible. > However, if one or more clients are still bound to the service, the > call to stop the service is delayed until all its clients unbind.

From Service ref:

> A service can be both started and have > connections bound to it. In such a > case, the system will keep the service > running as long as either it is > started or there are one or more > connections to it with the > Context.BIND_AUTO_CREATE flag. Once > neither of these situations hold, the > service's onDestroy() method is called > and the service is effectively > terminated.

Solution 3 - Android

If you start a service using startService(), then you should stop it using stopService().

> There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

You could bind to the service as many ServiceConnection as you want with bindService(), but pay attention to the flag you passed to it. If you pass 0 then if you call stopService() the service will stopped(i don't know exacltly what happens to you ServiceConnection). Otherwise if you want your service alive untill the ServiceConnection is binded to it then use BIND_AUTO_CREATE.

this is from stopService(): > Request that a given application service be stopped. If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started. > >Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle. > >This function will throw SecurityException if you do not have permission to stop the given service.

i hope this helps..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaxsapView Question on Stackoverflow
Solution 1 - AndroidFalmarriView Answer on Stackoverflow
Solution 2 - AndroidYuliia AshomokView Answer on Stackoverflow
Solution 3 - AndroidharaView Answer on Stackoverflow