"Sail Plan 036" 2015 sail plan Android Apidemo Devil Step App->service->messenger Service Messenger for interprocess communication

Source: Internet
Author: User

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

The following steps explain how to use Messenger:

    • Define a handler in the service to handle requests from the client.
    • Use this handler to create a messenger (containing a reference to handler).
    • Messenger creates a IBinder object that is returned to the client (the Onbind method).
    • The Client reconstructs a Messenger object using the IBinder returned from the service, providing the Messenger object to send a message to the service.
    • The 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 called directly by the client. Instead, the message is 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 to handle messages from the client.

    /*** Handler of incoming messages from clients. */    class Incominghandler extendsHandler {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseMSG_REGISTER_CLIENT:mClients.add (Msg.replyto);  Break;  CaseMSG_UNREGISTER_CLIENT:mClients.remove (Msg.replyto);  Break;  CaseMsg_set_value:mvalue=Msg.arg1;  for(intI=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 is going through the list from back to front//The so -is safe-to-do inside the loop.Mclients.remove (i); }                    }                     Break; default:                    Super. Handlemessage (msg); }        }    }

Then use this incominghandler to define a messenger:

    /**      * Target We publish for clients to send messages to Incominghandler.      */    Final New Messenger (new Incominghandler ());

The "Bound" service pattern should be used for this approach, Onbind needs to return a Ibind object that can be returned by Mmessenger.getbinder () to the IBinder object associated with the messenger. The client can reconstruct a Messenger object from this IBinder object, thus establishing a communication link with the service.

    /**      * When binding to the service, we return a interface to our Messenger     * for sending messages to the service.      */     @Override    public  ibinder onbind (Intent Intent) {        return Mmessenger.getbinder ();    }

Then look at the client code messengerserviceactivities in Serviceconnection's onserviceconnected definition, this method returns Messengerservice Onbind The IBinder object defined:

             Public voidonserviceconnected (componentname className, IBinder service) {//This was called when the connection with the service had been//established, giving us the service object we can use//interact with the service. We is communicating with our//service through a IDL interface, so get a client-side//Representation of that from the raw service object.Mservice =NewMessenger (service); Mcallbacktext.settext ("Attached."); //we want to monitor the service for as long as We are//connected to it.                Try{Message msg= Message.obtain (NULL, messengerservice.msg_register_client); Msg.replyto=Mmessenger;                                        Mservice.send (msg); //Give It some value as an example.msg = Message.obtain (NULL, Messengerservice.msg_set_value, This. Hashcode (), 0);                Mservice.send (msg); } Catch(RemoteException e) {//The service has crashed before we could even//Do anything with it; we can count on soon being//disconnected (and then reconnected if it can be restarted)//So there are no need to does anything here.                }                                //as part of the sample, tell the user what happened.Toast.maketext (Binding. This, r.string.remote_service_connected, Toast.length_short). Show (); }

This example implements two-way communication between the client and the service, so a Messenger object, Mmessenger, is defined in the client to handle messages from the service.

With the Mservice object, you can send a message to the service using send, and you can define the Message.replyto object if you need service return information.

"Sail Plan 036" 2015 sail plan Android Apidemo Devil Step App->service->messenger Service Messenger for interprocess 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.