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>
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.