0

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.

----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. The Manifest file is below

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ipc.android.server"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IPCServerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".booleanService">
            <intent-filter>
                <action android:name="ipc.android.server.booleanService">
                </action>
            </intent-filter>
            </service>
    </application>

   </manifest>
tshepang
  • 12,111
  • 21
  • 91
  • 136
sshasan
  • 27
  • 2
  • 11
  • Either put the AIDL files in a library project & include it in both projects or simply _duplicate_ it. AIDL is just an interface description that results in code generation - put in your `gen` folder. – Jens Jul 04 '12 at 23:08
  • @Jens I tried it with one way communication. I created a project and put the AIDL file in it. Changed the project to a library (checking the Is Library check box). Included that in both the IPC Server as well as the IPC Client's library reference. On compile time, the app exits with a NullPointerException. If you want, I can post the code. – sshasan Jul 05 '12 at 20:49
  • @Jens : I ve posted the code. – sshasan Jul 05 '12 at 21:15
  • Have you checked the return value of `bindService(new Intent("ipc.android.server.booleanService"), conn, Context.BIND_AUTO_CREATE)`, it should return `true` if your `booleanService` is correctly set up in your server application. If it returns false you need to go check your AndroidManifest.xml in the server app. – Jens Jul 06 '12 at 07:46
  • @Jens ok, so i checked. and as suspected, it is returning false. I have attached my Manifest file above. What do i check in it? (pardon my silly questions, I am really new at this) – sshasan Jul 06 '12 at 18:24
  • I don't see anything spontaneously wrong with your manifest declaration of the service - and you are using the action name consistently. So, what's the package name of the client? Since you've also defined a launcher Activity for the service, can you verify that it is still installed? – Jens Jul 06 '12 at 21:00
  • @Jens , BINGO! got it up and running. The one way at least. Silly me, – sshasan Jul 07 '12 at 16:09
  • @Jens , BINGO! got it up and running. The one way at least. Silly me,I totally forgot to install the other activity (sorry about that! I was too used to getting everything in the build path which installed things automatically). Ok, time to move on to two way communication. Thanks for taking the time to go through my code. I ll let you know if I have trouble with the two way communication. – sshasan Jul 07 '12 at 16:17
  • @Jens, tried out the two way thing. worked splendidly for me. Thanks :) – sshasan Jul 08 '12 at 23:33
  • @Jens, could you post your comment as an answer so that I can mark it as the one I used. – sshasan Jul 20 '12 at 03:34

1 Answers1

0

You only need to include the AIDL specs in both projects (no need to reference all of the other project). You can keep one copy of the AIDL and reference that one copy in both projects. If you're using Eclipse, find the build path properties and add the directory containing the AIDL as a source directory.

Anton Hansson
  • 2,141
  • 2
  • 14
  • 16
  • thanks! let me try this out, and i ll get back with the what happened . :) – sshasan Jul 04 '12 at 23:19
  • would you please explain, how do i add a directory as a source directory. Is it going to the project properties -> Java Build PAth -> Source Tab and then adding the folder? – sshasan Jul 05 '12 at 17:35