Android Apidemos Sample Resolution (a): App->service->messenger Service

Source: Internet
Author: User

The front LocalService primarily provides the same application components to use if you want to support different applications or processes using the service. You can use Messenger. Use Messgener can be used to support interprocess communication without using AIDL.

The following steps explain how Messenger is used:

Define a handler in the service to process requests from the client.

Use this handler to create a messenger (containing a reference to handler).

Messenger creates a IBinder object to return to the client (Onbind method).

The Client uses the IBinder returned from the service to reconstruct a Messenger object, providing the Messenger object to send messages to the service.

Service provides handler to accept message messages from the client. Provides handlemessage to process messages.

In this way, the service does not define a method that can be invoked directly by the client. Instead, messages are passed through "message".

This example Messenger Service involves two classes of messengerserviceactivities and Messengerservice.

First look at the definition of service, in Messengerservice defines a incominghandler for processing messages from the client.



*/
Class Incominghandler extends Handler {
@Override
public void Handlemessage (msg) {
Switch (msg.what) {
Case Msg_register_client:
Mclients.add (Msg.replyto);
Break
Case Msg_unregister_client:
Mclients.remove (Msg.replyto);
Break
Case Msg_set_value:
Mvalue = MSG.ARG1;
for (int i=mclients.size ()-1; i>=0; i--) {
try {
Mclients.get (i). Send (Message.obtain null,
Msg_set_value, Mvalue, 0));
catch (RemoteException e) {
The client is dead. Remove it from the list;
We are going through the list from back to front
So, safe to do inside the loop.
Mclients.remove (i);
}
}
Break
Default
Super.handlemessage (msg);
}
}
}

Then use this incominghandler to define a messenger.

Final Messenger Mmessenger = new Messenger (new Incominghandler ());

The "Bound" service pattern should be used for this method, Onbind need to return a Ibind object that can return the IBinder object associated with this Messenger through Mmessenger.getbinder (). The client can reconstruct a Messenger object through this IBinder object, thereby establishing a communication link with the service.

@Override public 
IBinder onbind (Intent Intent) {return
 mmessenger.getbinder ();     
}

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.