Android Apidemos Sample resolution: App->service->local Service Binding

Source: Internet
Author: User
Tags bind

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

LocalService can be used as a "started" service, or as a "Bound" service.

A "Bound" service can provide service through the Client/service mode. It runs an application component (such as an activity) that is bound to it, then accepts the request and returns a response or provides interprocess communication, and its lifecycle is usually the same as the component that invokes it, such as the activity. For LocalService, as a "started" service and as a "Bound" service, if LocalService has been started as a "started" service, even if all the client is disconnected from its binding, LocalService will not stop.

If a service needs to run other components to bind 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 bind to the "Bound" service through Bindservice, and the service and client bindings are implemented asynchronously, so the client needs to pass Serviceconnection Interface to monitor the connection to the service. When the client invokes Bindservice, Bindservice immediately returns, 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 the same application component and does not provide interprocess communication interfaces, so it is only necessary to derive a subclass of a binder if the direct function calls the interface.

Class for clients to access.  Because we know     
 //This service always runs     
 in the same process as//its clients, we don ' t need to deal with ipc.< C4/>public class Localbinder extends Binder {     
 LocalService getservice () {return     
 localservice.this;     
 }
}

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

LocalService's Onbind method definition:

@Override public 
IBinder onbind (Intent Intent) {return     
 mbinder;
}

This is the object of receives interactions from clients.     
Remoteservice for a complete example.     
Private final IBinder Mbinder = new Localbinder ();

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

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

Private LocalService Mboundservice;
Private Serviceconnection mconnection = new Serviceconnection () {
public void onserviceconnected (componentname className, IBinder service) {
This is called the connection with the service has been
Established, giving us the service object we can use to
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 void onservicedisconnected (ComponentName className) {
This is called the connection with the service has been
Unexpectedly disconnected--that's, its process crashed.
Because it is running into our same process, we should never
This happen.
Mboundservice = null;
Toast.maketext (Binding.this, r.string.local_service_disconnected,
Toast.length_short). Show ();
}
};

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.