Android service startService () and bindService ()

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

both services have different life cycles, so this is not possible, does anyone know about this?

+45
android
Aug 18 '10 at 16:25
source share
3 answers

I think the hara answer was a bit confusing. What you describe is completely legal and is actually the only way to achieve the desired behavior. If you create a service attached to it, it will die when you untie it. Thus, the only way to save it without binding to it is to start it using startService (). There is no conflict with life cycles, since it only applies to how the service STARTED. So, once it started with startService (), it follows the life cycle process. Thus, you can bind and untie it as much as you want, and it will die only when stopService () or stopSelf () is called

+79
Aug 18 '10 at 17:18
source share

If you start the service using startService (), you must stop it using stopService () .

There are two reasons why a system can manage a service. If someone calls Context.startService (), then the system will receive the service (by creating it and calling its onCreate () method, if necessary), and then calling its onStartCommand (Intent, int, int) method with arguments provided by the client . The service at this point will continue to work until Context.stopService () or stopSelf () is called. Note that several calls to Context.startService () are not nested (although they lead to several corresponding calls to onStartCommand ()), so no matter how many times it starts, the service will stop after Context.stopService () or stopSelf ( ) is called; however, services can use the stopSelf (int) method to ensure that the service will not be stopped until the started intentions are processed.

You can associate as many ServiceConnection as you like with bindService () , but pay attention to the flag that you passed to it. If you pass 0, then if you call stopService (), the service will stop (I don't know what the ServiceConnection will happen to you). Otherwise, if you want your service to be active as long as the ServiceConnection is not tied to it, use BIND_AUTO_CREATE .

this is from stopService ():

Request that this application service be stopped. If the service does not work, nothing happens. Otherwise, it will be stopped. Note that calls to startService () are not taken into account - this stops the service no matter how many times it has been started.

Note that if a stopped service still has ServiceConnection objects bound to it using BIND_AUTO_CREATE, it will not be destroyed until all these bindings are deleted. See the service documentation for more information on the service life cycle.

This function will raise a SecurityException if you do not have permission to stop this service.

Hope this helps.

+7
Aug 18 '10 at 16:44
source share

Yes, you can run and link (one or more times) the same service.

The following flowchart demonstrates how the service life cycle is managed. The variable counter keeps track of the number of related clients: enter image description here

A good example is a music application. Explanation of Creating a Media Browser Service Official Tutorial:

A service that is connected only (and not started) is destroyed when all its clients are disconnected. If your user interface is disconnected at this point, the service is destroyed. This is not a problem if you have not played any music yet. However, when playback begins, the user probably expects to continue listening even after switching applications. You do not want to destroy the player when you untie the interface to work with another application.

For this reason, you must be sure that the service starts when it starts playing by calling startService (). The initial service should be clearly stopped, regardless of whether it is connected. This ensures that the player continues to work even if the user interface activity monitoring is untied.

To stop a running 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 are untied.

From Service ref:

The service can be launched and related. In this case, the system will maintain the service while it is running or there is one or more connections to it using the .BIND_AUTO_CREATE Context. once none of these situations call the onDestroy () method and maintenance is effectively terminated.

+7
Feb 14 '17 at 7:45
source share



All Articles