Android uses Messenger-bound service for a variety of implementations _android

Source: Internet
Author: User

If you need to communicate between different processes, you can use Messenger in the service to implement communication in the process.

In this way, the service needs to define a handler object (responsible for responding to the message sent by the client).

Messenger can share the client with a IBinder object, and the client sends a message to the service through the IBinder object, and the handler object mentioned above is the basis of all this.

Note: Multithreading is not supported for communication in this manner.

So let's take a look at communication in this way!

Note: Service must be open when declared, that is, android:exported= "true" and this is a service initiated through intent, so the service can receive a specific action when declared.

1. Create a handler object in the service to process the message from the client.

2. Create a Messenger object based on the handler object you created

3, using Messenger's Getbinder method to get a IBinder object, and in the service Onbind method to reverse it

4. Client creates a Messenger object in onserviceconnected based on the IBinder parameter (refer to the Messenger's constructor)

5. The client can use the Messenger object from the previous step to send a message to the service

Through the top five we can have the client communicate with the service. After the client sends a message to the service using the Messenger object, the handler in the service responds to the messages.

It's just one-way communication, where the client sends a message to the service, and what if I need a service to send a message to the client?

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

6. Create a handler object in client to handle the message sent by service.

7. Create a client's own Messenger object based on the handler object in the client

8. In the 5th step, we obtained the Messenger object of the service and sent messages to the service through it. At this point, we assign the client's Messenger object to the ReplyTo field of the message object to be sent

9, in the service handler processing message will be client Messenger resolution, and use the client Messenger object to send messages to the client

This enables us to achieve two-way communication between client and service. Both client and service have their own handler and Messenger objects that allow the other to send messages to themselves, and it is noteworthy that the client Messenger is delivered to the service through the ReplyTo of the message.

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.