Using Messenger for cross-process communication in Android

Source: Internet
Author: User

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

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.