【发布时间】:2012-07-04 21:57:03
【问题描述】:
我有两个应用程序,我希望在它们之间交换一些数据。由于它们在不同的进程中运行,所以我使用 AIDL 在它们之间进行通信。现在,一切都在一个方向上发生得非常好(比如我的应用程序是 A 和 B),即数据正在从 A 发送到 B,但是现在我需要从 B 发送一些数据到 A。
我注意到我们需要将带有 AIDL 的应用程序包含在将调用 AIDL 方法的应用程序的构建路径中。因此,在我的情况下,A 在其构建路径中包含 B。为了让 B 能够向 A 发送一些东西,按照这种逻辑,B 在其构建路径中需要 A。这将创建一个循环。
我被困在这一点上。我想不出解决这个循环的方法。
----编辑----
所以,我遵循下面其中一个 cmets 中提到的建议,我有以下代码 在 IPCAIDL 项目中,AIDL 文件驻留, 它的内容是
package ipc.android.aidl;
interface Iaidl{
boolean pushBoolean(boolean flag);
}
这个项目被用作 IPCServer 和 IPC Client 中的库。
IPCServer 项目具有定义 AIDL 方法发生的情况的服务。 该文件是 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;
}
};
}
}
调用该方法的IPCClient文件是
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);
}
};
}
IPCServer 的清单文件包含服务的声明。清单文件在下面
<?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>
【问题讨论】:
-
要么将 AIDL 文件放入库项目中并将其包含在两个项目中,要么只是复制它。 AIDL 只是一个导致代码生成的接口描述 - 放在您的
gen文件夹中。 -
谢谢!我会试试这个,让你知道它是否有效!
-
@Jens 我尝试了一种方式的通信。我创建了一个项目并将 AIDL 文件放入其中。将项目更改为库(选中 Is Library 复选框)。包括在 IPC 服务器以及 IPC 客户端的库参考中。在编译时,应用程序以 NullPointerException 退出。如果你愿意,我可以发布代码。
-
@Jens :我已经发布了代码。
-
您是否检查过
bindService(new Intent("ipc.android.server.booleanService"), conn, Context.BIND_AUTO_CREATE)的返回值,如果您的booleanService在您的服务器应用程序中正确设置,它应该返回true。如果返回 false,您需要在服务器应用程序中检查您的 AndroidManifest.xml。