Messenger interprocess Communication

Source: Internet
Author: User

Messenger

Using Messenger, you can pass data between processes, enabling one-to-many processing. Its internal implementation is also based on the Aidl file, this aidl located in: Frameworks/base/core/java/android/os/imessenger.aidl.
The service side communicates with the client, mainly in the transmission message to do the processing, lets the Message.replyto point to the client Messenger, but the messenger also holds a binder object of the client, the service is correct to use this binder object and the client's pass Letter.

1. Service-Side implementation

Implements a Service class that defines the Messenger object that processes the message and returns the Binder object using the Getbinder () method of the Messenger object when Onbind.
When you create a Messenger object, you use a Handler object construct, which is used to process the messages sent by the client Handler.
If the server needs to return data to the client, remove the client's Messenger object (the ReplyTo parameter of the Message object) in the Message object and send it a message.

     Public classMyServiceextendsService {protected Static FinalString TAG = "Debug"; PrivateMessenger Mmessenger =NewMessenger (NewMessengerhandler ()); @Override Publicibinder onbind (Intent Intent) {returnMmessenger.getbinder (); }                Private Static classMessengerhandlerextendsHandler {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Casemsgconstants.messge_from_client:log.d (TAG,"Service Get Message"); Bundle Data=Msg.getdata (); String msgcontent= Data.getstring ("msg")); LOG.D (TAG,"" +msgcontent); //Reply to clientMessenger client =Msg.replyto; Bundle Replydata=NewBundle (); Replydata.putstring ("Reply", "your message <" + Msgcontent + ", I have received"); Message replymsg=Message.obtain (); Replymsg.what=Msgconstants.messge_from_service;                    Replymsg.setdata (Replydata); Try{client.send (replymsg); } Catch(RemoteException e) {log.e (TAG,"", E); }                     Break; default:                    Super. Handlemessage (msg);  Break; }            }        }    }    //Registration service in Andoidmanifest.xml<Service Android:name= "Com.example.MyService"android:exported= "true"android:process= ": Remote" > <intent-filter> <action android:name= "Com.example.MyService"/> </ Intent-filter> </service>

2. Client implementation

First, a bindservice is required, and in the Serviceconnection onserviceconnected () method, a Messenger object is constructed for the incoming IBinder object, which is used to send the server Send a message.
If you need to process the data returned by the server, you will also need to create another Messenger object. When sending data to the server, set the ReplyTo of the Message object to the Messenger object.

    //Connect ServiceIntent Service =NewIntent ("Com.example.MyService"); Service.setcomponent (NewComponentName ("Com.example", "Com.example.MyService"));            Bindservice (service, mconnection, service.bind_auto_create); PrivateMessenger Mservice; PrivateMessenger Mmessenger =NewMessenger (NewMessengerhandler ()); Private Static classMessengerhandlerextendsHandler {@Override Public voidhandlemessage (Message msg) {LOG.D (TAG,"Client Get Message"); Switch(msg.what) { CaseMsgConstants.MESSGE_FROM_SERVICE:Bundle Data=Msg.getdata (); LOG.D (TAG,"" + data.getstring ("Reply"));  Break; default:                Super. Handlemessage (msg);  Break; }        }    }        PrivateServiceconnection mconnection =Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {} @Override Public voidonserviceconnected (componentname name, IBinder service) {Mservice=NewMessenger (service);    }    }; //sending data to the server    Try{Message msg=Message.obtain (); Msg.what=msgconstants.messge_from_client; Bundle Data=NewBundle (); Data.putstring ("MSG", "KKKK");                Msg.setdata (data); //set up Messenger for service-side repliesMsg.replyto =Mmessenger;    Mservice.send (msg); } Catch(RemoteException e) {log.e (TAG,"", E); }

Messenger interprocess Communication

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.