Service Use binding:
There are two messengers that define a service side, Smessenger = new Messenger (new Handler () {});
Get the client's message in this messenger, get the messenger Cmessenger of the client in the received message,
Sending a message to a client is a message sent through the client Messenger Cmessenger.
Cmessenger.send (Message);
Note that a IBinder object is returned in Onbind (), Smessenger.getbinder ();
This is for the client to be able to obtain the service-side Messenger Smessenger.
Smessenger = new Messenger (IBinder) in the Serviceconnection interface;
The IBinder obtained here is the IBinder object obtained in Onbind ().
The client is sending a message to the server by obtaining the service Messenger Smessenger
Smessenger.send (msg); Send a message.
(1) Create a messenger on both the client and the service side,
The customer creates a cmessenger, and the service creates a Smessenger
(2) by means of obtaining a messenger of each other,
Get Smessenger in the client through Serviceconnection
Cmessenger = message.reply obtained on the server
(3) Send a message to each other via Messenger
Client: Smessenger.send (msg);
Service side: Cmessenger.send (msg);
Public classMyServiceextendsService { PublicMessenger Smessenger =NewMessenger (NewHandler () { Public voidhandlemessage (android.os.Message msg) {if(Msg.what = = 1){ intAge =Msg.arg1; String name=(String) msg.obj; //Get client Messenger Cmessenger = msg.replyto;//Get customer information through messages log.i ("Tag", name+ "--->" +Age ); Message reply=Message.obtain (); Reply.arg1= 1234; Reply.obj= name+ ">>>>" + "Roise"; Try{//send a message to a customer via client MessengerCmessenger.send (Reply); } Catch(RemoteException e) {e.printstacktrace (); } } }; }); Public voidonCreate () {LOG.I ("Tag", "Serviceoncreate ()"); }; @Override Publicibinder onbind (Intent Intent) {log.i ("Tag", "Onbind ()"); //get IBinder through the messenger of the service returnSmessenger.getbinder (); } @Override Public BooleanOnunbind (Intent Intent) {log.i ("Tag", "Onunbind ()"); return Super. Onunbind (Intent); } @Override Public voidOnDestroy () {LOG.I ("Tag", "Serviceondestroy"); Super. OnDestroy (); }}
Public classMainactivityextendsActivity {Messenger smessenger; Messenger Cmessenger=NewMessenger (NewHandler () { Public voidhandlemessage (android.os.Message msg) {//Get Service Short messageString info =(String) msg.obj; intAge =Msg.arg1; LOG.I ("Tag", "Message received by client" +info+Age ); }; }); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } protected voidOnStart () {Super. OnStart (); LOG.I ("Tag", "Activityonstart ()"); Intent Intent=NewIntent (mainactivity. This, MyService.class); Bindservice (Intent, Conn, bind_auto_create); }; protected voidOnDestroy () {LOG.I ("Tag", "Activityunbindservice ()"); Super. OnDestroy (); Unbindservice (conn); }; Public voidOnSend (View v) {Message msg=Message.obtain (); Msg.arg1= 12; Msg.obj= "Client";
//For two-way communication, send the client messenger to each other Msg.replyto = Cmessenger; Msg.what= 1; Try{smessenger.send (msg); } Catch(RemoteException e) {e.printstacktrace (); }} serviceconnection Conn=Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {} @Override Public voidonserviceconnected (componentname name, IBinder service) {
//Get service Messenger via Serviceconnection Smessenger = new Messenger (service); } };}
Android Service for four components (8) remote communication via Messenger