android Aidl原理分析

android Aidl原理分析

  • aidl的创建
  • server端使用
  • Client的使用

aidl是为了解决进程间通信的,也是binder的最好体现,下面分析一下:

先是aidl文件的创建

interface testtt{
    /**
     * 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 test(int dec);
}

第一个方法告诉我们binder通信支持的基本数据类型,会自动创建,第二个方法是我们定义的。
再来看server端:

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    Binder binder = new testttt.Stub(){

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public int test(int dec) throws RemoteException {
            return 0;
        }
    };
}

返回在client端绑定server的时候返回binder对象,而这个binder对象需要判断是否为跨进程。
最后看clent端:

ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            testttt test = testttt.Stub.asInterface(service);
            try {
                test.test(3);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

asInterface方法返回aidl接口类对象,而实现接口的是Myservice中的stub类,因此test.test方法最终调用的是Myservice中的stub类中的test方法。
因编译器自己生成的testtt.java文件,重点分析这个文件:

public interface testttt extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.uyun.testmap.testmap.testttt
{
private static final java.lang.String DESCRIPTOR = "com.uyun.testmap.testmap.testttt";
/** Construct the stub at attach it to the interface. */
public Stub()
{
	this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.uyun.testmap.testmap.testttt interface,
 * generating a proxy if needed.
 */
public static com.uyun.testmap.testmap.testttt asInterface(android.os.IBinder obj)
{
	if ((obj==null)) {
	return null;
	}
	android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
	if (((iin!=null)&&(iin instanceof com.uyun.testmap.testmap.testttt))) {
	return ((com.uyun.testmap.testmap.testttt)iin);
	}
	return new com.uyun.testmap.testmap.testttt.Stub.Proxy(obj);
}

我们看asInterface方法,IBinder 通过DESCRIPTOR查找aidl接口真正实现对象,若为本地引用,则直接返回,若为跨进程应用,返回Stub.Proxy代理对象,再进入:

private static class Proxy implements com.uyun.testmap.testmap.testttt
{
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 int test(int dec) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(dec);
mRemote.transact(Stub.TRANSACTION_test, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}

这里调用test方法,即调用mRemote.transact(Stub.TRANSACTION_test, _data, _reply, 0);,而mRemote是asInterface中的参数obj,即MyService中的参数binder,transact方法调用onTransact方法

/**
     * Default implementation rewinds the parcels and calls onTransact.  On
     * the remote side, transact calls into the binder to do the IPC.
     */
    public final boolean transact(int code, Parcel data, Parcel reply,
            int flags) throws RemoteException {
        if (false) Log.v("Binder", "Transact: " + code + " to " + this);

        if (data != null) {
            data.setDataPosition(0);
        }
        boolean r = onTransact(code, data, reply, flags);
        if (reply != null) {
            reply.setDataPosition(0);
        }
        return r;
    }

而onTransact方法的实现在stub类中。

@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_test:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
int _result = this.test(_arg0);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值