Android binding service-binding Service

Source: Internet
Author: User

The application component (client) can bind a service by calling the bindservice () method. Then, the android system calls the onbind () callback method of the service, this method returns an ibinder object that interacts with the server.

This binding is asynchronous. The bindservice () method returns immediately and does not return an ibinder object to the client. To receive an ibinder object, the client must create an instance of the serviceconnection class and pass the instance to the bindservice () method. The serviceconnection object contains a callback method that is called by the system to pass the ibinder object.

Note:Only the activity, service, and content provider can bind a service-a broadcast receiver cannot bind a service.

Therefore, to bind a service to a client, you must do the following:

1. Implement serviceconnection abstract class

Your implementation must rewrite the following two callback methods:

Onserviceconnected ()

The system will call this method to send the ibinder object returned by the onbind () method of the service.

Onservicedisconnected ()

The Android system calls this method when the connected service is suddenly lost, such as when the service crashes or is killed. This method is not called when the client unbinds the service.

2. Call the bindservice () method to pass the implementation of the serviceconnection class;

3. When the system calls your onserviceconnected () callback method, you can start to use the method defined in the interface to call the service;

4. Call the unbindservice () method to disconnect from the service.

When the client is destroyed, it will be unbound from the server. However, when you complete interaction with the server or the activity is paused, you should actively unbind it from the server, so that the service can be turned off when it is not used. (The time for binding and unbinding will be discussed later ).

For example, the following code snippet demonstrates connecting the client to create a service by inheriting the binder class. All it does is to convert the ibinder object returned by the server to the LocalService class and request the LocalService instance:

LocalService mservice;
Private serviceconnection mconnection = new serviceconnection (){
// Called when the connection with the service is established
Public void onserviceconnected (componentname classname, ibinder Service ){
// Because we have bound to an explicit
// Service that is running in our own process, we can
// Cast its ibinder to a concrete class and directly access it.
Localbinder binder = (localbinder) service;
Mservice = binder. getservice ();
Mbound = true;
}

// Called when the connection with the service disconnects unexpectedly
Public void onservicedisconnected (componentname classname ){
Log. E (TAG, "onservicedisconnected ");
Mbound = false;
}
};

With this serviceconnection class, the client can pass it to the bindservice () method to bind it to the server, for example:

Intent intent = new intent (this, LocalService. Class );
Bindservice (intent, mconnection, context. bind_auto_create );

1. The first parameter of the bindservice method is an intent object, which explicitly names the service to be bound (although intent can be implicitly specified );

2. The second parameter is the serviceconnection object;

3. The third parameter indicates the identifier of the binding option. Usually it should use bind_auto_create to create this service when the service does not exist. Other possible values are bind_debug_unbind and bind_not_foreground, or none of them are "0 ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.