Android cross-process Messenger (easy to understand) _android

Source: Internet
Author: User
Tags stub

No need for aidl or complex contentprovider, no need to sharedpreferences or shared storage files!

Just a straightforward messenger, also known as a messenger, allows you to pass message objects in different processes, where you can communicate and pass data across processes by placing the data we need to pass through. Nonsense not much to say, directly on the code.

First is the service side:

public class Ser extends service{
@Override public
ibinder onbind (Intent Intent) {
return Messenger.getbinder ();
}
@Override public
int Onstartcommand (Intent Intent, int flags, int startid) {
//TODO auto-generated method Stub
   LOG.I ("Service", "Onstartcommand ()");
Return Super.onstartcommand (Intent, flags, Startid);
}
Public Messenger messenger = new Messenger (new MyHandler ());
The public class MyHandler extends handler{
@Override
the public void Handlemessage (msg) {
log.i (" Ser---TAG "," msg:: "+msg.arg1+" Want: "+msg.getdata (). getString (" msg ");
Messenger messenger = Msg.replyto;
Message message = Message.obtain (null, 0);
Bundle Bundle = new Bundle ();
Bundle.putstring ("Reply", "Well, your message I have received, reply you later!");
Message.setdata (bundle);
try {
messenger.send (message);
} catch (RemoteException e) {
e.printstacktrace ();
}
Super.handlemessage (msg);
}

We did not do much on the server, just instantiated a messenger and created a handler to receive messages sent by the client

Next look at the client:

public class Client extends service{private static final String TAG = "Client"; protected Messenger mservice; Dler handler = new Handler () {public void Handlemessage (msg) {LOG.I ("Client---TAG", "msg:;"
+msg.getdata (). getString ("reply"));
};
};
Public Messenger messenger = new Messenger (handler); @Override public IBinder onbind (Intent Intent) {return null;} @Override public int onstartcommand (Intent Intent, int fla GS, int startid) {Intent mintent = new Intent (); Mintent.setclassname ("Com.example.test1", "Com.example.test1.Ser"); bin
Dservice (Mintent, Mbindservice, context.bind_auto_create);
Return Super.onstartcommand (Intent, flags, Startid); @Override public void OnDestroy () {Super.ondestroy (); Unbindservice (Mbindservice);} private Serviceconnection Mbindse Rvice = new Serviceconnection () {@Override public void onserviceconnected (componentname name, IBinder service) {Mservice
= new Messenger (service);
Message message = Message.obtain (null, 0); Bundle Bundle= new Bundle ();
Bundle.putstring ("msg", "Hello this is client!");
Message.replyto = Messenger;
Message.setdata (bundle);
try {mservice.send (message);} catch (RemoteException e) {e.printstacktrace ()}}
@Override public void onservicedisconnected (componentname name) {//TODO auto-generated Method stub}}; }

The same client also needs a handler to receive the message returned by the server, and there is a key point

When the client sends a message, it needs to send the messenger of the receiving service back through the

ReplyTo parameter is passed to the service side, otherwise it will be reported to NullPointerException. And then we're looking at the log.

"Hello this are client!" is the client sent to the service side, proof that the server has received!

"Well, I have received your message, and will reply to you later!" This is the server to return to the client, prove that the client also received, and still real-time communications oh, to this our cross-process transmission of data communication complete end, is not very simple!

Related Article

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.