The most straightforward cross-process communication (Messenger) in Android history!

Source: Internet
Author: User

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

Just the simple Messenger, which is also known as the Messenger, through which you can pass a message object in a different process, put the data that we need to pass in the message and you can communicate and pass data across processes. Nonsense not much to say, directly on the code.

The 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 ());
public class MyHandler extends handler{
@Override
public void Handlemessage (Message 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're not doing much on the server, just instantiating a messenger and creating 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;
Public Handler Handler = new Handler () {
public void Handlemessage (Message 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 flags, int startid) {
Intent mintent = new Intent ();
Mintent.setclassname ("Com.example.test1", "com.example.test1.Ser");
Bindservice (Mintent, Mbindservice, context.bind_auto_create);
Return Super.onstartcommand (Intent, flags, Startid);
}
@Override
public void OnDestroy () {
Super.ondestroy ();
Unbindservice (Mbindservice);
}
Private Serviceconnection Mbindservice = 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 a key point

When the client sends a message, it needs to send the messenger that receives the server reply through the message

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

"Hello this is client!" which is sent to the server by the client and proves that the server has received it!

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

The most straightforward cross-process communication (Messenger) in Android history!

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.