Android bound service details 3: Use messenger

Source: Internet
Author: User

If you need your service to communicate with remote processes, you can use a messenger to provide interfaces for your service. This technology can be used to implement inter-process communication (IPC) without using aidl ).

The following is an overview of how to use MESSENGER:

  • Service implements a handler that receives the callback caused by each call from the client.

  • Handler is used to create a messenger object (it is a reference of handler ).

  • Messenger creates an onbind () from the service and returns it to the ibinder of the client.

  • The client uses ibinder to instantiate this messenger (it references the handler of the service), and the client uses it to send a message to the service.

  • The Service receives each message in its handler-specific, In the handlemessage () method.

In this way, no method can be called by the client in the service. The client sends the "message" (message object) received by the Service in its handler ).

The following is an example of using the messenger interface for a service:

Public class messengerservice extends Service {<br/>/** command to display a message for the Service */<br/> static final int msg_say_hello = 1; </P> <p>/** <br/> * processes messages from the client. <br/> */<br/> class incominghandler extends handler {<br/> @ override <br/> Public void handlemessage (Message MSG) {<br/> switch (MSG. what) {<br/> case msg_say_hello: <br/> toast. maketext (getapplicationcontext (), "Hello! ", Toast. length_short ). show (); <br/> break; <br/> default: <br/> super. handlemessage (MSG ); <br/>}</P> <p>/** <br/> * We publish it to the client so that it can send messages to incominghandler message object <br/> */<br/> final messenger mmessenger = new messenger (New incominghandler ()); </P> <p>/** <br/> * When bound to a service, we return the interface pointing to our messenger <br/> */<br/> @ override <br/> Public ibinder onbind (intent) {<br/> toast. maketext (getapplicationcontext (), "binding", toast. length_short ). show (); <br/> return mmessenger. getbinder (); <br/>}< br/>


Note that the handlemessage () method in handler is where the Service receives and processes messages.

All the client has to do is create a messenger object based on the ibinder returned by the service and send a message using its send. for example, the following example shows how to bind an actvity to a service and send the msg_say_hello command to the service:

Public class activitymessenger extends activity {<br/>/** messenger that communicates with the Service */<br/> messenger mservice = NULL; </P> <p>/** indicates whether the tag is bound to the Service */<br/> Boolean mbound; </P> <p>/** <br/> * class for interacting with the main interface of the Service <br/> */<br/> private serviceconnection mconnection = new serviceconnection () {<br/> Public void onserviceconnected (componentname classname, ibinder Service) {<br/> // called when the connection to the service has been established. Provided the objects that can be used to <br/> // interact with the service. we are using a messenger to communicate with the service, <br/> // here, we obtain the messenger representative of a client from the original ibinder object. <br/> mservice = new messenger (service ); <br/> mbound = true; <br/>}</P> <p> Public void onservicedisconnected (componentname classname) {<br/> // called when the connection to the service is accidentally disconnected -- that is, the service process crashes <br/> mservice = NULL; <br/> mbound = false; <br/>}< br/>}; </P> <p> Public void sayhello (view v) {<br /> If (! Mbound) return; <br/> // create and send a message to the service. <br/> message MSG = message. obtain (null, messengerservice. msg_say_hello, 0, 0); <br/> try {<br/> mservice. send (MSG); <br/>}catch (RemoteException e) {<br/> E. printstacktrace (); <br/>}</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 service <br/> bindservice (new intent (this, messengerservice. class), mconnection, <br/> context. bind_auto_create); <br/>}</P> <p> @ override <br/> protected void onstop () {<br/> super. onstop (); <br/> // unbind from the service <br/> If (mbound) {<br/> unbindservice (mconnection); <br/> mbound = false; <br/>}< br/>}

Note that this example does not demonstrate how the service responds to the client. if you want the Service to respond to the client, you need to create a messanger on the client. then, when the client receives the onserviceconnected () callback, it sends an information to the service. The message contains the client's messenger object, which serves as the replyto parameter of the send () method.

Compare aidl

When you need to execute IPC, it is easier to use a messenger for your interface than to use aidl, because messenger puts all service calls into the queue, A pure aidl interface sends requests to the Service in parallel, so that multi-thread processing is required.

For most applications, the Service does not need to use multithreading, so using a messenger allows the service to process only one request at a time. if multithreading is important to your service, you should use aidl to define your interface.



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.