Binding service three ways of using Messenger for process two-way communication

Source: Internet
Author: User

Ideas:

(The following service-side referred to as the server, access to the service-side referred to as the client)

1. the message is communicated between the service process and the client process, so the server must have a handler object to handle the received Message object.

2. The Messenger object used by the client to send a message object to the server is transmitted from the service side. Similarly, if the server sends a message to the client, the messenger used is delivered from the client. That is, one end of the response message is to provide the Messenger Messenger object to the opposite side

3. The client sends a message to the server before the server can send the message to the client in response.

4. Note: If two-way communication is achieved, both the server and the client have handler object and Messenger object, the handler object handles the message and Messenger sends the message.

This article binds the service with an implicit intent, and you need to set the action value of intent, which corresponds to the Action property set in Intent-filter in the server-side configuration file. You need to register the service in the configuration file and set the exported property to True, and the default value of this property is true.false that the external process cannot access the service.

Implementation steps:

1. Create a service process, create a handler object in the service, handle the message sent by the client, and use the handler object to create a Messenger object for the client to send messages to the server.

2, create the IBinder interface object in the Onbind () method with the Messenger object created in the previous step, and pass the value of the Onbind () method back to the client

3. Register the service in the configuration file and register a filter.

4. Create a client process, create a subclass of the implementation Serviceconnection interface on the client, implement two methods of the interface, in onserviceconnected (ComponentName arg0, IBinder arg1) {} method, A Messager object is created using the IBinder interface object parameter passed by the server to send a message to the client.

This enables a one-way communication that the client sends a message to the server.

5, if two-way communication is required, create your own handler object on the client side, and use the object to create the Messenger object of the client.

6. When the client side send message, the Messenger object created in the previous step is assigned to the ReplyTo field of the message;

7. When the service side processes the message, it obtains the client-side Messenger, which is encapsulated in messages, and the Messenger object can be used to send a message to the customer service side.

Binding service three ways of using Messenger for process two-way communication

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.