AIDL 的理解&源码分析

本文详细探讨了AIDL(Android Interface Definition Language)在移动端IPC通信中的作用,讲解了对象如何实现Parcelable接口,以及Binder驱动在通信过程中的角色。重点阐述了Proxy作为IPC通讯的代理,asInterface()方法在查找本地接口中的应用,以及在bindService时ServiceConnection的使用。同时,文章提到了ServiceManager如何作为Binder驱动的入口,服务于客户端和服务端之间的通信。
摘要由CSDN通过智能技术生成

aidl通讯试所有的对象都得实现Parcelable 接口

onTransact() Binder 驱动执行完毕时需要回调的方法

Proxy 移动端IPC通讯的时候,消息都是通过Binder 进行传递的。
这个代理可以理解为:IPC机制中,Android代码 跟底层对应IPC的Binder驱动 之间进行通讯时的代理

asInterface() 查询本地所有的aidl 接口,判断该Binder对象是否为一个本地的 Binder引用

然后bindService的时候,需要传递一个 ServiceConnection的内部类实现,在ServiceConnection内部类实现中
bindService(intent, conn, BIND_AUTO_CREATE);


    ServiceConnection conn=new ServiceConnection() {    
        //aidl链接失败回调
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
        //aidl连接成功回调
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //在系统内部将执行一下逻辑行为
            /*public static com.aidl.lxcay.MyAidlInterface asInterface(android.os.IBinder obj) {
            * if ((obj == null)) {
            *   return null;
            * }
            * 查询本地所有的aidl 的描述,判断该Binder对象是否为一个本地的 Binder引用
            * android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            * if (((iin != null) && (iin instanceof com.aidl.lxcay.MyAidlInterface))) {
            * 返回定义好的代理接口
            * return ((com.aidl.lxcay.MyAidlInterface) iin);
            * }
            * 返回默认代理
            * return new com.aidl.lxcay.MyAidlInterface.Stub.Proxy(obj);
            * }
            */
            //这个localInterface 就是aidl里面规定好的接口引用,即Binder 引用
            localInterface = Stub.asInterface(service);
            //在客户端可以通过点击事件执行这个引用里对应的函数,如果执行成功,会在相应的Service里面执行相应的 函数;
            //下面给出一个demo
        }
    };
    eg:
    //客户端
    public void click(View v) throws RemoteException{
        Log.i(TAG, localInterface.login("24", 24)+"");

        Log.i(TAG, localInterface.sayHello(new Person("lxcay", 24))+"");
    }
    //服务端
    public class MyServices extends Service {
   
        @Override
        pub
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值