This is a mirror of official site: http://jasper-net.blogspot.com/

InterAppCommunication

| Thursday, May 26, 2011
Introduction
This tutorial covers the mechanism for performing Inter-App Communication. It comes with two apps. A client app that makes the request and a service app that processes the request and sends a response back.

Step 1: Create the Service
You must create a service that will be bound to before invocation. The service must return an IBinder object during the onBind invocation

public class RemoteService extends Service
{
        private Messenger messenger; //receives remote invocations
        
        @Override
        public IBinder onBind(Intent intent) 
        {
                if(this.messenger == null)
                {
                        synchronized(RemoteService.class)
                        {
                                if(this.messenger == null)
                                {
                                        this.messenger = new Messenger(new IncomingHandler());
                                }
                        }
                }
                //Return the proper IBinder instance
                return this.messenger.getBinder();
        }
The service uses a Messenger and sends it binder back. A Messenger is the component that makes inter process communication possible.

Step 2: Implement the Handler
Handler is the component that is registered with the Messenger. This component is the one that receives the remote invocation.

private class IncomingHandler extends Handler
        {
                @Override
        public void handleMessage(Message msg) 
                {
                        System.out.println("*****************************************");
                        System.out.println("Remote Service successfully invoked!!!!!!");
                        System.out.println("*****************************************");
                        
                        int what = msg.what;
                        
                        Toast.makeText(RemoteService.this.getApplicationContext(), "Remote Service invoked-("+what+")", Toast.LENGTH_LONG).show();
                        
                        //Setup the reply message
                        Message message = Message.obtain(null, 2, 0, 0);
                        try
                        {
                                //make the RPC invocation
                                Messenger replyTo = msg.replyTo;
                                replyTo.send(message);
                        }
                        catch(RemoteException rme)
                        {
                                //Show an Error Message
                                Toast.makeText(RemoteService.this, "Invocation Failed!!", Toast.LENGTH_LONG).show();
                        }
        }
        }

This Handler processes the incoming request and sends a reply based on the replyTo Messenger that comes in with the incoming request.

Full Src for the Service implementation

Read more: openmobster

Posted via email from Jasper-net

0 comments: