AIDL

我们使用aidl非常方便,只要定义了aidl文件,系统便会帮我们处理好了一切,现在我们就来了解一下编译过程中,系统到底是怎么帮我们编译成java代码的

首先,定义aidl文件

interface IBridgeService {
    void doSomething();
}

然后编译,会在build/source/aidl/debug/pagename生成IBridgeService文件
生成的文件如下

public interface IBridgeService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.xiaozhou.aidl_demo.IBridgeService
{
private static final java.lang.String DESCRIPTOR = "com.xiaozhou.aidl_demo.IBridgeService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.xiaozhou.aidl_demo.IBridgeService interface,
 * generating a proxy if needed.
 */
public static com.xiaozhou.aidl_demo.IBridgeService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.xiaozhou.aidl_demo.IBridgeService))) {
return ((com.xiaozhou.aidl_demo.IBridgeService)iin);
}
return new com.xiaozhou.aidl_demo.IBridgeService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_doSomething:
{
data.enforceInterface(DESCRIPTOR);
this.doSomething();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.xiaozhou.aidl_demo.IBridgeService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public void doSomething() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_doSomething, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_doSomething = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void doSomething() throws android.os.RemoteException;
}

接下来,我们来分享这个文件
首先,生成的文件的类名,跟我们定义接口名是一样的,方法也都是我们定义的,不过多出了异常的抛出
有个抽象的内部静态类Stub,继承于android.os.Binder
Stub有个静态内部类Proxy,实际上的操作由这个类处理

我们来看看asInterface方法

    /**
     * Cast an IBinder object into an com.xiaozhou.aidl_demo.IBridgeService interface,
     * generating a proxy if needed.
     */
    public static com.xiaozhou.aidl_demo.IBridgeService asInterface(android.os.IBinder obj) {
        if ((obj == null)) {
            return null;
        }
        android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
        if (((iin != null) && (iin instanceof com.xiaozhou.aidl_demo.IBridgeService))) {
            return ((com.xiaozhou.aidl_demo.IBridgeService) iin);
        }
        return new com.xiaozhou.aidl_demo.IBridgeService.Stub.Proxy(obj);
    }

如果在同一进程,则直接返回,不在同一进程则初始化一个,这里用了Proxy去代理
也就是说,我们实现stub,然后系统通过Proxy代理执行我们的stub,并实现Binder的通信

然后剩下的就是Binder的相关知识啦

你可能感兴趣的:(AIDL)