Use of the Messenger class

Source: Internet
Author: User

First, the Messenger class

Function: Similar to the message class, but is used across processes.

Analysis: Its bottom layer is implemented by AIDL, from the construction method can be seen

Service usage

Public Messenger (Handler target) {

Mtarget = Target.getimessenger ();

}

Client Side use

Public Messenger (IBinder Targer) {

Mtarget = IMessenger.Stub.asInterface (target);

}

Second, use (Service and client send each other information)

Step one: Create Messenger on the service side

 Public classBookserviceextendsService {Private Static FinalString TAG = "Bookservice"; //①: Create handler to prepare for Messenger creation    PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Switch(msg.what) { Case0:                    //when the client sends the message, the output
Bundle bundle = Msg.getdata (); LOG.D (TAG, "service received message" +bundle.getstring ("MSG")); Break; } } }; //②: Create Messenger PrivateMessenger Mmessenger =NewMessenger (Mhandler); @Override Public voidonCreate () {Super. OnCreate (); } @Override Publicibinder onbind (Intent Intent) {//③: Returns the binder of the Messenger object to the client returnMmessenger.getbinder (); }}

Step two: Client side receive Messenger send message to service side

 Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity"; PrivateMessenger Mservice; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Serviceconnection Connection=Newserviceconnection () {@Override Public voidonserviceconnected (componentname name, IBinder service) {//re-create MessengerMservice =NewMessenger (service); //get the message from the MSG poolMessage msg = Message.obtain (NULL, 0); //Create BundlesBundle bundle =NewBundle (); Bundle.putstring ("MSG", "Hello,nice to meet"); //to load bundles into a message objectMsg.setdata (bundle); Try {                    //Sends a message to the server, stating that the message inherits the Parcelable interfacemservice.send (msg); } Catch(RemoteException e) {e.printstacktrace (); }} @Override Public voidonservicedisconnected (componentname name) {}}; Intent Intent=NewIntent ( This, Bookservice.class);    Bindservice (Intent, connection, bind_auto_create); }

Step three: Implement the server to send information to the client two-way communication

//Client Side/*** Step One: Create a message sent by the handler receiving server.*/    PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Switch(msg.what) { Case0: Bundle Bundle=Msg.getdata (); LOG.D (TAG,"Client received message:" +bundle.getstring ("Replydata")));  Break; }        }    };/*** Step Two: Add in public void onserviceconnected (componentname name, IBinder service) method*/Msg.replyto= Mservice;
/** handler in the *service end */ // receive Messenger        sent from client in handler Messenger messenger = msg.replyto;          // Create msg         Message replymessage = Message.obtain (null, 0);          Try {           // send msg back to client          messenger.send (replymessage);          Catch (RemoteException e) {                 e.printstacktrace ();         }

Flow chart

Use of the Messenger class

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.