AIDL based two way communication

Posted by sshasan on Stack Overflow See other posts from Stack Overflow or by sshasan
Published on 2012-07-04T21:57:03Z Indexed on 2012/07/05 21:15 UTC
Read the original article Hit count: 227

Filed under:
|
|
|

I have two apps between which I want some data exchanged. As they are running in different processes, so, I am using AIDL to communicate between them. Now, everything is happening really great in one direction (say my apps are A and B) i.e. data is being sent from A to B but, now I need to send some data from B to A.

I noticed that we need to include the app with the AIDL in the build path of app where the AIDL method will be called. So in my case A includes B in its build path. For B to be able to send something to A, by that logic, B would need A in its build path. This would create a cycle.

I am stuck at this point. And I cannot think of a work around this loop.

Any help would be greatly appreciated :) .

Thanks!

----EDIT----

So, I following the advice mentioned in one of the comments below, I have the following code In the IPCAIDL project the AIDL file resides, its contents are

package ipc.android.aidl;
interface Iaidl{
        boolean pushBoolean(boolean flag);
 }

This project is being used as a library in both the IPCServer and the IPC Client.

The IPCServer Project has the service which defines what happens with the AIDL method. The file is booleanService.java

 package ipc.android.server;

 import ipc.android.aidl.Iaidl;
 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.util.Log;

   public class booleanService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return new Iaidl.Stub() {

        @Override
        public boolean pushBoolean(boolean arg0) throws RemoteException {
            Log.i("SERVER(IPC AIDL)", "Truth Value:"+arg0);
            return arg0;
        }
    };
  }

  }

The IPCClient file which calls this method is

   package ipc.android.client2;

   import ipc.android.aidl.Iaidl;
   import android.app.Activity;
   import android.content.ComponentName;
   import android.content.Context;
   import android.content.Intent;
   import android.content.ServiceConnection;
   import android.os.Bundle;
   import android.os.IBinder;
   import android.os.RemoteException;
   import android.view.View;
   import android.widget.Button;

   public class IPCClient2Activity extends Activity {

Button b1;
Iaidl iAIDL;
boolean k = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bindService(new Intent("ipc.android.server.booleanService"), conn, Context.BIND_AUTO_CREATE);
    startService(new Intent("ipc.android.server.booleanService"));

    b1 = (Button) findViewById(R.id.button1);

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(k){
                k = false;
            }
            else{
                k = true;
            }
            try {
                iAIDL.pushBoolean(k);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
        }
    });
}

private ServiceConnection conn = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        iAIDL = Iaidl.Stub.asInterface(service);
    }
};
  }

The manifest file for IPCServer includes the declaration of the service.

© Stack Overflow or respective owner

Related posts about android

Related posts about ipc