Let's talk about how to use the Bindservice () binding servicethe application component (client) can call Bindservice () to bind to a service. The Android system then calls the service's Onbind () method, which returns a ibinder binding that is used to interact with the service asynchronously. Bindservice () returns immediately, and it does not return IBinder to the client. To receive IBinder, the client must create an instance of Serviceconnection and passto Bindservice (). The serviceconnection contains a callback method that is called by the system to pass the IBinder to be returned.
Note: Only activities,services, and contentproviders can bind to a service-you cannot bind from one broadcastreceiver to the service.
Broadcastreceiver 's context life cycle is short, and bindservice has little meaning.
Bindservice Steps1 Implement Serviceconnection.
Your implementation must rewrite the two callback methods:
Onserviceconnected ()
The system calls this to transmit the IBinder returned in the service's Onbind ().
Onservicedisconnected ()
The Android system calls this when the connection to the service is accidentally lost. For example, when the service crashes or is strongly killed. This method is not invoked when the client is unbound.
2 call Bindservice () and pass it to the serviceconnection implementation.
3 When the system calls your onserviceconnected () method, you can start invoking the service using the method of the interface definition.
4 to disconnect from the service, call Unbindservice ().
When your client is destroyed, it will unbind from the service, but you must always unbind when you have finished interacting with the service or when your activity is paused so that the service can be turned off in the case of not being used. (There are more questions about binding and unbinding at the appropriate time.)
With this serviceconnection, the client can bind to a service by passing it to Bindservice (). For example:
Intentintent = new Intent (this, localservice.class);
Bindservice (Intent,mconnection, context.bind_auto_create);
The first bindservice () parameter is a intent that explicitly specifies the service to bind to.
The second argument is a Serviceconnection object.
The third parameter is a flag that indicates the action in the binding. It should generally be bind_auto_create, which creates a service that does not exist. Other optional values are Bind_debug_unbind and bind_not_foreground, which you do not want to specify when you set it to 0.
Additional Items
Here are some important things to do with binding to a service:
you always need to catch deadobjectexception exceptions. It will be thrown when the connection is interrupted. This is the only exception thrown by the remote method.
The object reference count is a cross-process effect.
You should make the binding and unbind pairing during the lifetime of the client, for example:
If you need to interact with the service when your activity is visible, you should bind in OnStart () and Unbind in OnStop ().
If you want your activity to receive responses even when it stops, you can bind in OnCreate () and Unbind in OnDestroy (). Note that this means that your activity needs to use the service during its entire run (even if it is in the background), so if the service is in another process, you increase the burden on the process and make it easier for the system to get killed.
Note: You should generally not bind and unbind to the service in your activity's Onresume () and OnPause () because these callback methods appear in each life-time change, and you need to minimize the processing that occurs in these changes. Also, if multiple activity in your app is bound to the same service, and a change occurs between the two activity, the service may be in the current Activity Unbind (pause) and before the next binding (Rusume) Be destroyed and rebuilt.
Manage (multiple clients) lifetime of Boundservicewhen all clients of a service are unbound, the Android system destroys it (unless it is started from Onstartcommand ()). If your service is a pure boundservice, you do not need to manage its lifecycle-android system will manage it for you.
However, if you choose to implement the Onstartcommand () callback method, you must explicitly stop the service because the service is now considered "started". In this case, the service runs until the service uses stopself () to stop it itself or another component calls StopService () to stop it, regardless of whether a client has bound it. Also, if your service has started and accepts bindings, then when the system calls your Onunbind () method, you can choose to return true to indicate that you want to accept a call to Onrebind () when the next time the client binds to the service ( Instead of a call to Onbind (). Onrebind () returns void, but the client still receives IBinder in its onserviceconnected () callback. Demonstrates the logic of this life:
Bindservice and StartService when used in combination1. If Bindservice first, then StartService:
When bind activity exits, the service executes the Unbind method without executing the Ondestory method, because there is a StartService method called, So when the activity and service unbind, there is a service presence that is not connected to the caller.
2. If you bindservice first, then StartService, then call Context.stopservice
The Ondestory method of the service does not execute immediately, because there is an activity bound to the service, but when the activity exits, the ondestory is executed, and if StopService is executed immediately, the binding must be unbound first.
Put the above "if first Bindservice, then StartService" Replace "if first StartService, then Bindservice", the result is the same
When a service is not destroyed by Ondestory (), only the first client that initiates it can invoke its onbind () and Onunbind ().
Welcome to scan QR Code, follow public account
Bindservice () and StartService () mixed with Android service in-depth analysis