The process property of the server-side messengerservice specifies that its processes and mainactivity are not in a process
<service android:name="com.example.activity.MessengerService" android:process="com.example.activity.remote" > </service>
Create a messenger in Messengerservice with its underlying binder as the IBinder object returned by the binding service.
privatefinalnew Messenger(new messengerHandler()); @Override publiconBind(Intent intent) { return messenger.getBinder(); }
The Messengerhandler is used to process the message sent by the client to the server via Messenger.
privateclass messengerHandler extends Handler{ @Override publicvoidhandleMessage(Message msg) { super.handleMessage(msg); System.out.println(msg.getData().getString("msg")); } }
The service is bound in the client mainactivity.
Intent intent = new Intent(); intent.setClass(MainActivity.this, MessengerService.class); bindService(intent, conn, Context.BIND_AUTO_CREATE);
The binding service requires Serviceconnection to create Messenger to bind the IBinder object returned by the service, sending a server to the client to send the information.
PrivateServiceconnection conn =NewServiceconnection () {@Override Public void onservicedisconnected(componentname name) { }@Override Public void onserviceconnected(componentname name, IBinder service) {Messenger Messenger =NewMessenger (service); Message msg = Message.obtain (); Bundle data =NewBundle (); Data.putstring ("MSG","Hello!!!"); Msg.setdata (data);Try{messenger.send (msg); }Catch(RemoteException e) {E.printstacktrace (); } } };
At this point, the server can accept the message sent by the client through Messengerhandler, thus completing the communication between the processes.
Of course, the server can also reply to the message to the client.
A messenger can also be created on the client side to process messages sent by the server via Hanlder.
privatenew Messenger(new MessengerHandler());privateclass MessengerHandler extends Handler{ @Override publicvoidhandleMessage(Message msg) { super.handleMessage(msg); System.out.println(msg.getData().getString("reply")); } }
Of course, you need to pass the client's mainmessenger to the server, and the server sends the message to the client via Mainmessenger. This is passed using the ReplyTo property of the message.
msg.replyTo = mainMessenger;
The server can then return the message to the client via Messenger delivered by the client.
Messenger client = Msg.rep Lyto Bundle bundle = new Bundle () Message reply = Message.obtain () Bundle.putstring ( "reply" , " Hello yeah! ") Reply.setdata (bundle) ; try {client.send (Reply) } catch (RemoteException e) {e.printstacktrace () }
The client can then obtain the message sent back by the server via Mainmessenger.
Using Messenger communication does not handle high concurrency very well, because its messages are handled one by one. It transmits messages through a message, so only the bundle data type is supported. This can be used in low-concurrency, instant communications.
Using Messenger for cross-process communication in Android