Messenger of Android interprocess communication

Source: Internet
Author: User

When you talk about interprocess communication, everyone will think Aidl, in fact, Messenger and aidl function, can be used for inter-process communication. It is a message-based interprocess communication, just like a sub-thread and a UI thread sending a message, isn't it simple to write a aidl file, or is it a little cool? Ha ha.

In addition, it supports logging of client object Messenger, you can then implement a one-to-many communication, and even as a transfer point, any two processes can communicate through the server.

Compare with aidl :

when you need to perform IPC , it is easier to use Messenger for your interface than to use the Aidl implementation, because Messenger queues all service calls and pure aidl The interface sends multiple requests to the service at the same time, and the service must then deal with multithreading.

for most applications, the service does not need to perform multithreading, so use Messenger allows the service to process one call at a time. If your service must perform multithreading, you should use aidl to define the interface.

Next look at how to write:

Service side:

1. Create a handler object and implement the Hanlemessage method for receiving messages from the client and processing

2. Create a Messenger ( Messenger), package Handler

3.messenger creates a IBinder object that is returned to the client by Onbind

Client:

1. Binding Services in activity

2. Create serviceconnection and use ibinder to instantiate Messenger in it

3. use Messenger to send messages to the server

4. Unbind Service

5. receive each Message in the Handlemessage () method in the server

this way, the client does not invoke the "method" of the service. The message object, which is passed by the client, is received by the service in its Handler .

The above implementation is only one-way communication, that is, the client sends a message to the server, if I need the server to send a message to the client and how to do it?

In fact, this is also very easy to achieve, let us follow the above steps to achieve two-way communication bar

1. Create a Handler object in the client to handle messages sent from the service side

2. Create a client-side Messenger object and encapsulate the handler.

3. Assign the client Messenger object to the ReplyTo field of the message object to be sent

4. The client's messenger is parsed when the message is processed by the Handler on the server and a message is sent to the client using the client's Messenger object

This enables two-way communication between the client and the server.

Note: The Service must be open at the time of declaration, i.e. android:exported= "true"; In addition, I will use some test the APP's online automated test tool:www.ineice.com


Messenger of Android interprocess communication

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.