The easiest cross-process communication (Messenger) in the history of android )!, Androidmessenger

Source: Internet
Author: User

The easiest cross-process communication (Messenger) in the history of android )!, Androidmessenger

No need for AIDL, complex ContentProvider, SharedPreferences, or shared storage files!

You only need easy-to-understand Messenger, which is also called a Messenger. It can be used to transmit message objects in different processes, put the data we need to transmit in the message, you can implement cross-process communication and data transmission. Let's talk about the code.

The first is the server:

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, I have received your message and will reply to you later! ");
Message. setData (bundle );
Try {
Messenger. send (message );
} Catch (RemoteException e ){
E. printStackTrace ();
}
Super. handleMessage (msg );
}
}

We operate a little more on the server, just instantiate a Messenger, and create a handler to receive messages sent from the client.

Next, let's 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 client also needs a handler to receive the messages returned by the server. It is also very important

When the client sends a Message, the Messenger that receives the reply from the server must pass the Message

The replyTo parameter is passed to the server; otherwise, NullPointerException is reported. Then, let's take a look at the log.

"Hello this is client! "This is sent from the client to the server, proving that the server has received it!

"Well, I have received your message and will reply to you later! "This is what the server returns to the client, proving that the client has also received and is still communicating in real time. At this point, our cross-process data transmission is complete, isn't it easy!

 

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.