"Sail Plan 034" 2015 sail plan Android Apidemo Devil Step app->service->local Service Binding binding services Serviceconnection Binder

Source: Internet
Author: User

This example and the activity code for the following local Service Controller are defined in Localserviceactivities.java, as implemented by the Localserviceactivities inner class. The service invoked is LocalService.

LocalService can be either a "Started" service or a "Bound" service.

A "Bound" service can provide service through Client/service mode. It runs an application component (such as activity) to bind to it, then accepts the request and returns the response or provides an interprocess communication mechanism whose life cycle is usually the same as the component that called it (such as activity). And for LocalService as "Started" service and as "Bound" service, if LocalService has been started as "Started" service, even if all the client disconnects from it, LocalService will not stop.

If a service needs to be bound to run other components as a "Bound" service, the Onbind method must be implemented. This method returns a Ibound object to the client. The client can invoke the service's method through this Ibind object.

The client can use Bindservice to bind to the "Bound" service, and the service-to-client binding is implemented asynchronously, so the client needs to pass the Serviceconnection Interface to monitor the connection to the service. When the client calls Bindservice, Bindservice immediately returns and then calls Serviceconnection when the Android system establishes a link between the client and the service. Onserviceconnection to inform the client and service that the link was established successfully and to return the client to the Ibind object provided by the service.

LocalService is only available to components of the same application and does not provide interprocess communication interfaces, so it is only necessary to derive a binder subclass if the direct function calls the interface.

// Class for clients to access.   //  //Itsclients, we don ' t need to deal with IPC.  Public class extends  return LocalService.  This ; }}

Localbinder only provides a method getservice that returns the LocalService object itself.

The Onbind method definition for LocalService:

    @Override    public  ibinder onbind (Intent Intent) {        return  Mbinder;    }     // This is the object of the receives interactions from clients.  See    //  Remoteservice for a more complete example.    Private Final New Localbinder ();

The return value of Onbind is Mbinder for the Localbinder class object. It defines a method GetService.

Then look at the implementation of the client class Localserviceactivities.binding:

        PrivateLocalService Mboundservice; PrivateServiceconnection mconnection =Newserviceconnection () { Public voidonserviceconnected (componentname className, ibinder service) {//This was called when the connection with the service had been//established, giving us the service object we can use//interact with the service. Because we have bound to a explicit//service that we know are running in our own process, we can//cast its ibinder to a concrete class and directly access it.                Mboundservice = ((localservice.localbinder) service). GetService (); //Tell the user about this for our demo.Toast.maketext (Binding. This, r.string.local_service_connected, Toast.length_short). Show (); }             Public voidonservicedisconnected (componentname className) {//This was called when the connection with the service had been//unexpectedly disconnected--that's, its process crashed. //Because It is running in our same process, we should never//See this happen.Mboundservice =NULL; Toast.maketext (Binding. This, r.string.local_service_disconnected, Toast.length_short). Show (); }        };

Mconnection defines the Serviceconnection interface, onserviceconnected, which is called when onservicedisconnected establishes a link and disconnects between the service and the client respectively. Where the IBinder service is the return object of the service's onbind. This converts it through the type to LocalService, which is mboundservice.

The Client establishes a binding to the service by calling Bindservice:

        voidDobindservice () {//establish a connection with the service. We Use an explicit//class name because we want a specific service implementation that//We know'll is running in our own process (and thus won ' t is//supporting component replacement by other applications).Bindservice (NewIntent (Binding. This, LocalService.class), mconnection, context.bind_auto_create); Misbound=true; }

Call Unbindservice If you need to break the binding to the service.

        void Dounbindservice () {            if  (misbound) {                //  Detach Our existing Connection.                 Unbindservice (mconnection);                 false ;            }        }

If you are familiar with socket programming, Client-bound "Bound" Service methods are very similar to using socket communication.

"Sail Plan 034" 2015 sail plan Android Apidemo Devil Step app->service->local Service Binding binding services Serviceconnection Binder

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.