如何通过Aidl进行跨进程间的服务调用?

Android interface Definition Language
如何通过Aidl进行跨进程间的服务调用?
在server端需要三步:
1) 创建一个aidl文件;
Package com.example.test
Interface AppConnect {
Void connect(String key, String value);
}
在定义好aidl之后,eclipe集成的ADT会编译直接在gen目录下生成AppConnect.java,googel在其中做不了少事,有兴趣的同学,可以看看;
2) 实现定义的接口;
private AppConnect.Stub mConnectBinder = new AppConnect.Stub() {

@Override
public void connect(String key, String value) throws RemoteException {
debugLog("key:"+key+" value:"+value);
}
};
3) 将你定义的接口显露给外部;
在实现好接口后,还需要提供给外部接口,而这里的接口就是(2)中的mConnectBinder。重载Service的OnBinder()接口,提供接口给外部:
public IBinder onBind(Intent arg0) {
debugLog("onBind mConnectBinder:"+mConnectBinder);
return mConnectBinder;
}
到此服务器端的工作完成,下面是client端的调用过程:
4) 在manifest.xml的Service中加入intent-filter,这个好像也可以不用;
<intent-filter>
<action android:name="com.example.test.AppConnectService"/>
</intent-filter>

Client端:
1) BindService,获得Serive接口;
static final String serviceActionName = " com.example.test.AppConnectService ";

private Context context = null;
private AppConnect mService = null;

public void ConnectStatService(Context ctx){
Log.d(TAG,"ConnectStatService");
context = ctx;
if (context != null) {
Intent intent = new Intent(serviceActionName);
context.bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
}
}

public void DisConnectStatService(){
Log.d(TAG,"DisConnectStatService");
if (context != null) {
context.unbindService(mConnection);
}
}

private ServiceConnection mConnection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name){
Log.d(TAG,"disconnect from service");
mService = null;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = AppConnect.Stub.asInterface(service);
if(mService == null){
Log.d(TAG,"connect failed");
return;
}
Log.d(TAG,"finish invoking AIDL");
}
};
2) 接口调用
try {
mService.connect(appkey, value);
} catch (RemoteException e) {
e.printStackTrace();
}

总的来说,还是比较装简单的。具体的原理,暂没有细看。后续补充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AIDL(Android Interface Definition Language)是一种用于 Android 平台的接口定义语言,它可以帮助不同进程的组件进行通信。下面是使用 AIDL 进行进程通信的步骤: 1.定义 AIDL 接口 首先,在服务端和客户端之定义一个 AIDL 接口。在 AIDL 文件中,定义需要向客户端公开的方法和参数。 2.实现 AIDL 接口 在服务端中,实现定义的 AIDL 接口,并在 onCreate() 方法中将其注册到系统中。 3.绑定服务端 在客户端中,使用 bindService() 方法绑定服务端。 4.获取 AIDL 接口实例 在客户端中,实现 ServiceConnection 接口,当服务端连接成功时,会回调 onServiceConnected() 方法。在此方法中,可以获取到 AIDL 接口实例。 5.调用 AIDL 接口方法 在客户端中,通过获取到的 AIDL 接口实例,即可调用服务端暴露的方法。 下面是一个简单的示例代码: 服务端: ``` //定义 AIDL 接口 interface IMyAidlInterface { int add(int a, int b); } //实现 AIDL 接口 class MyAidlInterfaceImpl extends IMyAidlInterface.Stub { @Override public int add(int a, int b) throws RemoteException { return a + b; } } //在 onCreate() 方法中注册 AIDL 接口 @Override public void onCreate() { super.onCreate(); Intent intent = new Intent(this, MyAidlInterfaceService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); Log.i(TAG, "MyAidlInterfaceService is created."); } //定义 ServiceConnection 对象,以便在客户端连接时获取 AIDL 接口实例 private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); Log.i(TAG, "MyAidlInterfaceService is connected."); } @Override public void onServiceDisconnected(ComponentName name) { mIMyAidlInterface = null; Log.i(TAG, "MyAidlInterfaceService is disconnected."); } }; ``` 客户端: ``` //定义 ServiceConnection 对象 private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //获取 AIDL 接口实例 mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); Log.i(TAG, "MyAidlInterfaceService is connected."); } @Override public void onServiceDisconnected(ComponentName name) { mIMyAidlInterface = null; Log.i(TAG, "MyAidlInterfaceService is disconnected."); } }; //使用 bindService() 方法绑定服务端 Intent intent = new Intent(); intent.setComponent(new ComponentName("com.example.myaidlservice", "com.example.myaidlservice.MyAidlInterfaceService")); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); //调用服务端暴露的方法 int result = mIMyAidlInterface.add(1, 2); ``` 希望这个简单的示例可以帮助你了解如何使用 AIDL 进行进程通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值