版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1340341
AIDL(Android Interface Definition Language)是Android系统自定义的接口描述语言。可以用来实现进程间的通讯。
在 Android 中,要实现进程间的通讯,一般来说,有以下几种方式:
最常见的的是我们通过特定的 Action 或者 data 启动另外一个应用的 Activity 或者 service。我们可以将要传递的数据封装在 bundle 当中。
两个应用读取某个文件,从而达到进程通讯的问题,不过这种方法需要处理好文件锁的问题,不然很容易引发数据错乱。
Messenger 进行进程间的通讯是串行的,而且是单向的,如果客户端和服务端想进行双向通讯,需要维护两个 Messenger,相对比较麻烦
aidl 一般用来进程通讯。一般来说,主要有两种角色,客户端 (Client)和服务端(Server)。
一般用来处理客户端的请求,他把与客户端通讯的方式抽象成接口,并编写成 AIDL 文件。
通常服务端需要实现一个 Service,来处理客户端的请求
通常我们需要将服务端 的 AIDL 文件 copy 过来,并通过 Intent 的方式来启动我们服务端的 Service。
接下来让我们一起来实现一个简单功能,通过一哥 app(Clilent) 唤起另外一个 APP (Server),并进行两者之间的通讯。首先,我们先来看一下服务端的实现。
服务端的实现,一般来说,需要以下步骤:
一般来说, AIDL 文件支持以下类型
注意事项
in 表示输入参数,即服务端可以修改该类型
out 表示输出参数,即客户端可以修改该类型,客户端不行
inout 表示客户端和服务端都可以修改该类型
如
void onSuccess(int code,in MusicInfo musicInfo);
有人可能会这样想,既然 inout 表示客户端和服务端都可以修改该类型,那我们平时在写 aidl 文件的时候,直接在方法参数前面加上 inout 修饰就 OK了,省得去区分。
这样做法当然不行,既然双方都可以修改,那系统的开销肯定会比较大。就好比管道一样。
说了这么多,接下来让我们一起来看一下例子 IEasyService.aidl
package xj.musicserver.easy;
// Declare any non-default types here with import statements
interface IEasyService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void connect(String mes);
void disConnect(String mes);
}
这个 aidl 文件很接口,只有两个方法,connect 和 disConnect 方法。
这里我们把 aidl 文跟 Java 文件中放在一起,需要在 build.gradle 中配置
sourceSets {
main {
jniLibs.srcDirs = ['libs']
aidl.srcDirs = ['src/main/java']
}
}
关于怎样在 AndroidStudio 中引用 aidl 文件的,可以参考我的这一篇文章 AndroidStudio 引用 aidl 文件的两种方法
public class EasyService extends Service {
private static final String TAG = "EasyService";
public EasyService() {
}
IEasyService.Stub mIBinder=new IEasyService.Stub() {
@Override
public void connect(String mes) throws RemoteException {
LogUtil.i(TAG,"connect: mes =" + mes);
}
@Override
public void disConnect(String mes) throws RemoteException {
LogUtil.i(TAG, "disConnect: mes =" +mes);
}
};
@Override
public IBinder onBind(Intent intent) {
LogUtil.i(TAG,"onBind: intent = "+intent.toString());
return mIBinder;
}
@Override
public boolean onUnbind(Intent intent) {
LogUtil.i(TAG,"onUnbind: =");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
LogUtil.i(TAG,"onDestroy: =");
super.onDestroy();
}
}
这个 Service 所做的动作就是当客户端连接上的时候,会启动我们的 Service,此时会调用 Service 的 onBInd 方法,在 onBinder 方法里面,我们将 mIBinder (实现了 IEasyService.Stub 接口)返回回去。他充当客户端和服务端的桥梁,通过他我们可以进行通讯。
至于这个 IEasyService.Stub 是什么呢?其实当我们在 AndroidStudio 里面编写完 aidl 文件,重新 make project 一下,就会自动生成了。
在 AndroidManifest 文件下,配置 Service 的 action 及 exported 等信息。
其中 android:exported=”true” 表示别的进程可以访问,这个是必须配置的。android:process=”:remote” 表示运行在 :remote 进程,不配置的话默认运行所在的 App 进程,这个可以不配置。
<service
android:name=".easy.EasyService"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="xj.musicserver.easy.IEasyService"/>
</intent-filter>
</service>
第一步:将服务端的 aidl 文件 copy 过来,注意要放在同一个包下
当我们点击按钮的时候,我们通过 Action 去启动远程 servic。
case R.id.btn_start_service:
LogUtil.i(TAG,"onButtonClick: btn_start_service=");
Intent intent = new Intent(ACTION);
// 注意在 Android 5.0以后,不能通过隐式 Intent 启动 service,必须制定包名
intent.setPackage(XJ_MUSICSERVER);
bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
private static final String ACTION = "xj.musicserver.easy.IEasyService";
private IEasyService mIEasyService;
ServiceConnection mServiceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIEasyService = IEasyService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mIEasyService=null;
}
};
测试将会启动远程 Service,及服务端的 EasyService,并调用 onBind 方法,打印出相应信息。
xj.musicserver:remote I/MusicInterface: EasyService :onBind: intent = Intent { act=xj.musicserver.easy.IEasyService pkg=xj.musicserver }
第三步:如果想与服务端通讯,通过保存下来的 Binder,即可调用服务端的方法。
比如当我们点击按钮的时候,调用 connect 方法。
case R.id.btn_contact:
LogUtil.i(TAG,"onButtonClick: btn_contact=");
if(mIEasyService!=null){
mIEasyService.connect(" Cilent connect");
}
此时将会调用远程 service 的 mIbinder 的 connect 方法,log 中输出
xj.musicserver:remote I/MusicInterface: EasyService :connect: mes = Cilent connect