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