Android bound service description 2: derivative binder class

Source: Internet
Author: User

If your service is only used by your own application and does not need to work across processes, You can implement your own binder class so that your client can directly use the public interface method of the service.

Note: This only works when the client and service are in the same application and process. This is the case in most cases. for example, this method works well when a music application needs to bind its activity to its own background service for playing music.

The following is how to create it:

  1. In your service, creating a binder instance provides one of the following three functions:

  • The binder contains some public methods that can be called by the client.

  • Returns the current service instance, which has some public methods that can be called by clients.

  • Or, return an instance of another class, which has a public method called by the client and is hosted on the service.

  1. The binder instance is returned in the onbind () callback method.

  2. On the client side, receive the binder from the callback method onserviceconnected () and call the binding service using the public method described in 1.

Note: The reason why the service and client must be in the same application is that the client can correctly convert the returned object and call its public method. the service and client must be in the same process, because it does not have to perform cross-process sending.

For example, the following service provides a function for the client to call methods in the service through a binder:

Public class LocalService extends Service {<br/> // binder given to clients <br/> private final ibinder mbinder = new localbinder (); <br/> // random number generator <br/> private final random mgenerator = new random (); </P> <p>/** <br/> * class used for client binder. because we know that this <br/> * service always runs in the same process as the client, we do not need to process IPC. <br/> */<br/> public class localbinder extends binder {<br/> LocalService getservice () {<br/> // return the instance of this service to the client, therefore, the client can call the public method of this service <br/> return LocalService. this; <br/>}</P> <p> @ override <br/> Public ibinder onbind (intent) {<br/> return mbinder; <br/>}</P> <p>/** method to be called by the client */<br/> Public int getrandomnumber () {<br/> return mgenerator. nextint (100); <br/>}< br/>

The class localbinder provides the getservice () method so that the client can obtain the current LocalService instance. the client can call the public method in the service. for example, the client can call the getrandomnumber () method of the service.

The following is an example of actvity that is bound to LocalService and called getrandomnumber () when the button is pressed:Public class bindingactivity extends activity {<br/> LocalService mservice; <br/> Boolean mbound = false; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}</P> <p> @ override <br/> protected void onstart () {<br/> super. onstart (); <br/> // bind to an instance of the LocalService class <br/> intent = new intent (this, LocalService. class); <br/> bindservice (intent, mconnection, context. bind_auto_create); <br/>}</P> <p> @ override <br/> protected void onstop () {<br/> super. onstop (); <br/> // unbind from service <br/> If (mbound) {<br/> unbindservice (mconnection); <br/> mbound = false; <br/>}</P> <p>/** called when the button is pressed (the button defined in the layout file is used in Android: onclick attribute specifies the response to this method) */<br/> Public void onbuttonclick (view v) {<br/> If (mbound) {<br/> // call a method of LocalService <br/> // However, if the call has a pending operation, then this request should be sent <br/> // generated in another thread to avoid reducing the activity performance. <br/> int num = mservice. getrandomnumber (); <br/> toast. maketext (this, "Number:" + num, toast. length_short ). show (); <br/>}</P> <p>/** defines the callback bound to the service and sends it to bindservice () */<br/> private serviceconnection mconnection = new serviceconnection () {</P> <p> @ override <br/> Public void onserviceconnected (componentname classname, <br/> ibinder Service) {<br/> // We have bound to LocalService to forcibly convert the ibinder type and obtain the LocalService instance. <br/> localbinder binder = (localbinder) service; <br/> mservice = binder. getservice (); <br/> mbound = true; <br/>}</P> <p> @ override <br/> Public void onservicedisconnected (componentname arg0) {<br/> mbound = false; <br/>}< br/>}; <br/>}

The preceding example shows how the client binds an instance of serviceconnection to the service using the onserviceconnected () method.

Note: The above example does not explicitly unbind from the service. However, all clients should unbind from the service when appropriate (for example, when the activity is paused ).

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.