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 ();
}