AIDL是很老的知识点了,网上资料一大堆,查查资料看看就明白了,但是一段时不用就忘了,故此篇博客记录一下AIDL的一般写法,以备不时之需。
下面开始
aidl是应用程序间进行通信的一个桥梁,一般需要一个Service充当服务端,另外一个应用绑定这个Service,并且通过binder驱动来调用服务端提供的方法。Service在绑定的时候需要返回一个IBinder对象,故绑定service的目的也是架通客户端与服务端的桥梁—IBinder。
- 服务端:
废话不多说,当时要先建一个aidl文件,方法是在main目录下新建一个aidl目录(java同级目录),然后随便写一个包名,然后再用AndroidStudio新建一个aidl文件(注意本篇介绍都是基于AndroidStudio)。
package com.zhg.demo.aidl;
// Declare any non-default types here with import statements
interface ICalcInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
int add(int a,int b);
int sub(int a,int b);
}
basicTypes()这个方法是新建文件时的默认方法,不去管它。你删掉即可
服务端是一个service,主要重写onBind()方法代码如下:
@Override
public IBinder onBind(Intent intent) {
Log.e("info","aidl.Server.onBind()=============");
return myBinder;
}
myBinder的代码在此:
private ICalcInterface.Stub myBinder=new ICalcInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public int add(int a, int b) throws RemoteException {
return a+b;
}
@Override
public int sub(int a, int b) throws RemoteException {
return a-b;
}
};
还有一部,注册这个Service,AndroidManifest配置如下:
<service android:name=".CalcService" android:enabled="true" android:exported="true">
<intent-filter >
<action android:name="com.zhg.demo.aidl.service"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</service>
注意我们给这个Service设置了一个Action,在客户端就通过这个Action来启动此Service
服务端完成,接着进行客户端代码编写
- 客户端
客户端也是新建一个aidl文件,你可以把这个东东理解成一个接口,规范并提供了一组方法供客户端调用。注意aidl的包名与文件名必须与服务端的aidl的保持一致。
package com.zhg.demo.aidl;
// Declare any non-default types here with import statements
interface ICalcInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
int add(int a,int b);
int sub(int a,int b);
}
接下来就客户端绑定service,调用服务的方法,首先来看下启动service的代码,都是些常规代码:
绑定service
Intent intent=new Intent("com.zhg.demo.aidl.service");
bindService(intent,mServiceConnection , Context.BIND_AUTO_CREATE);
mServiceConnection对象在此:
private ICalcInterface iCalcInterface;
private ServiceConnection mServiceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.e("info","aidl.Client.onServiceConnected()======");
iCalcInterface=ICalcInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e("info","aidl.Client.onServiceDisconnected()==========");
iCalcInterface=null;
}
};
调用加法运算方法
if(iCalcInterface!=null){
try {
int result=iCalcInterface.add(100,200);
Toast.makeText(MainActivity.this,"result="+result,Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this,R.string.tips,Toast.LENGTH_SHORT).show();
}
减法运算类似,故略
注意:bindService后,即使unbind了service,还是能调用Server提供的服务的,除非在后台强行停止程序。
源码地址:https://github.com/naiyizhang/BlogDemo
AIDLServer和AIDLClient模块