Use Messenger to bind services in three ways

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.

If this method is used, the Service needs to define a Handler object (responsible for responding to the Message sent from the client ).

Messenger can share an IBinder object with the client. The client sends a Message to the Service through this IBinder object, and the Handler object mentioned above is the basis of all this.

Note: using this method for communication does not support multithreading.

Let's see how to use this method for communication!

Note: The Service must be open to the outside when it is declared, that is, android: exported = "true". In this article, the Service is started through Intent. Therefore, the Service can receive specific actions when declaring the Service.

1. Create a Handler object in the Service to process the Message sent from the client.

2. Create a Messenger object based on the Handler object created

3. Use the getBinder method of Messenger to get an IBinder object and reverse it in the onBind method of Service.

4. The client creates a Messenger object based on the IBinder parameter in onServiceConnected (refer to the Messenger constructor)

5. The client can use the Messenger object obtained in the previous step to send a Message to the Service.

After the above five steps, we can allow the client to communicate with the Service. After the client uses the Messenger object to send a Message to the Service, the Handler in the Service will respond to the Message.

The above only implements one-way communication, that is, the client sends messages to the Service. What should I do if the Service needs to send messages to the client?

In fact, this is also very easy to implement. Let's proceed with the above steps to implement two-way communication ~

6. Create a Handler object in the client to process messages sent by the Service.

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

8. We obtained the Messenger object of the Service in step 1 and sent a message to the Service through it. At this time, we assign the client's Messenger object to the replyTo field of the Message object to be sent.

9. parse the client's Messenger when the Service's Handler processes the Message, and use the client's Messenger object to send the Message to the client.

In this way, two-way communication between the client and the Service is realized. Both the client and Service have their own Handler and Messenger objects so that the other party can send messages to themselves. It is worth noting that the client's Messenger is passed to the Service through the Message replyTo.

Demo link: http://pan.baidu.com/share/link? Consumer id = 583593 & uk = 2953765628

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.