我在[003]AIDL是什么中介绍的AIDL,但是好像还有朋友不明白问我,那我就来写一个终极版的文章,让你十分钟彻底明白AIDL,以下代码全为手写。
目标
Server进程注册一个Binder服务到SM,该Binder服务提供两个接口:add和minus Client 进程通过SM获得Binder服务的代理类BinderProxy,并调用两个接口add,minus
1 没有AIDL的世界
1.1 Server进程
代码语言:javascript复制 class CommandBinder extends Binder {
@Override
protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags) throws RemoteException {
if (code == 1) {
int a = data.readInt();
int b = data.readInt();
reply.writeInt(a b);
return true;
} else if (code == 2) {
int a = data.readInt();
int b = data.readInt();
reply.writeInt(a b);
return true;
}
return super.onTransact(code, data, reply, flags);
}
}
void main() {
//注册binder服务
ServiceManager.addService("command", new CommandBinder());
}
1.2 Client进程
代码语言:javascript复制 int add(int a, int b) {
int code = 1;
BinderProxy mClient = ServiceManager.getService("command");
Parcel data = Parcel.obtain();
data.writeInt(a);
data.writeInt(b);
Parcel reply = Parcel.obtain();
mClient.transact(code, data, reply, 0);//flag设置成0,调用这个方法会跨进程调用Binder服务类中onTransact方法
int result = reply.readInt();
return result;
}
int minus(int a, int b) {
int code = 2;
BinderProxy mClient = ServiceManager.getService("command");
Parcel data = Parcel.obtain();
data.writeInt(a);
data.writeInt(b);
Parcel reply = Parcel.obtain();
mClient.transact(code, data, reply, 0);//flag设置成0,调用这个方法会跨进程调用Binder服务类中onTransact方法
int result = reply.readInt();
return result;
}
2 有AIDL的世界
AIDL文件
代码语言:javascript复制 //aidl
interface ICommand {
int add(int a, int b);
int minus(int a, int b);
}
2.1 Server进程
与1.1中代码比较,我们发现,我们不是直接继承Binder,而是继承aidl文件自动生成ICommandBinder,我们只需专注写add和minus接口的实现,不需要在Binder类中的onTransact写一堆if else代码和Parcel.read write.
代码语言:javascript复制 class ADILCommandBinder extends ICommandBinder {
@Override
public int add(int a, int b) {
return a b;
}
@Override
public int minus(int a, int b) {
return a - b;
}
}
void main() {
//注册binder服务
ServiceManager.addService("command", new ADILCommandBinder());
}
2.2 Client进程
与1.2中代码比较,我们发现,我们不需要写Parcel.read write和BinderProxy.transact,而是用BinderProxy对象构造一个CommandBinderProxy对象,然后直接调用CommandBinderProxy的add和minus
代码语言:javascript复制 int add(int a, int b) {
BinderProxy mClient = ServiceManager.getService("command");
return new CommandBinderProxy(mClient).add(a, b);
}
int minus(int a, int b) {
BinderProxy mClient = ServiceManager.getService("command");
return new CommandBinderProxy(mClient).minus(a, b);
}
3 ADIL自动生成了什么代码
生成了三个部分:ICommand,ICommandBinder,CommandBinderProxy
3.1 ICommand
看起来和aidl文件差不多
代码语言:javascript复制 interface ICommand {
int add(int a, int b);
int minus(int a, int b);
}
3.2 ICommandBinder
继承于Binder,并实现ICommand,然后在onTransact方法中调用还没有实现的add和minus方法
代码语言:javascript复制 abstract public class ICommandBinder extends Binder implements ICommand {
int add = 1;
int minus = 2;
@Override
protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags) throws RemoteException {
if (code == 1) {
int a = data.readInt();
int b = data.readInt();
reply.writeInt(add(a, b));
return true;
} else if (code == 2) {
int a = data.readInt();
int b = data.readInt();
reply.writeInt(minus(a, b));
return true;
}
return super.onTransact(code, data, reply, flags);
}
abstract public int add(int a, int b);
abstract public int minus(int a, int b);
}
3.3 CommandBinderProxy
对BinderProxy进行封装,把调用CommandBinderProxy的add转化成Parcel.write,read和BinderProxy.transact代码。
代码语言:javascript复制 public class CommandBinderProxy implements ICommand {
private BinderProxy remote;
int add = 1;
int minus = 2;
public CommandBinderProxy(BinderProxy remote) {
this.remote = remote;
}
@Override
public int add(int a, int b) {
Parcel data = Parcel.obtain();
data.writeInt(a);
data.writeInt(b);
Parcel reply = Parcel.obtain();
remote.transact(add, data, reply, 0);
int result = reply.readInt();
return result;
}
@Override
public int minus(int a, int b) {
int code = 2;
Parcel data = Parcel.obtain();
data.writeInt(a);
data.writeInt(b);
Parcel reply = Parcel.obtain();
remote.transact(code, data, reply, 0);
int result = reply.readInt();
return result;
}
}
总结
看完应该明白了AIDL作用主要有 a.偷懒,少很多代码,尤其是你要写上百个方法的时候,AIDL就派上大用场了。 b.让服务端更专注接口的实现,而减少犯错误的可能性 c.规范client端和server端的接口定义,有助于代码的迭代
当然这是我自己写的伪代码,AIDL真正生成的代码比较复杂,但这些伪代码就是AIDL核心点。
思考
oneway的这个语法对AIDL生成的代码有什么影响,或者你们自己写一下oneway的方法AIDL生成的伪代码