Android AIDL分析

首先给大家分享一个巨牛巨牛的人工智能教程,是我无意中发现的。教程不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵段子,像看小说一样,哈哈~我正在学习中,觉得太牛了,所以分享给大家!点这里可以跳转到教程

               

1. AIDL接口

    对于WindowManagerService,如果client需要访问它,需要通过AIDL接口来访问,其接口在IWindowManager.aidl中定义,ADIL接口在编译时将生成一个IWindowManager.java文件(此文件在out目录下),其主要相关代码如下:  

/** * System private interface to the window manager service. * * {@hide} */ public interface IWindowManager extends android.os.IInterface{    ...    public static abstract class Stub extends android.os.Binder implements android.view.IWindowManager    {        ...        /**        * Cast an IBinder object into an android.view.IWindowManager interface,        * generating a proxy if needed.        */        public static android.view.IWindowManager asInterface(android.os.IBinder obj)        {            if ((obj==null)) {                return null;            }            android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);            if (((iin!=null)&&(iin instanceof android.view.IWindowManager))) {              return ((android.view.IWindowManager)iin);            }            return new android.view.IWindowManager.Stub.Proxy(obj);        } //end asInterface        ....        private static class Proxy implements android.view.IWindowManager        {            private android.os.IBinder mRemote;            Proxy(android.os.IBinder remote)            {                mRemote = remote;            }            public android.os.IBinder asBinder()            {                return mRemote;            }            ...            public android.view.IWindowSession openSession(com.android.internal.view.IInputMethodClient client, com.android.internal.view.IInputContext inputContext) throws android.os.RemoteException            {                android.os.Parcel _data = android.os.Parcel.obtain();                android.os.Parcel _reply = android.os.Parcel.obtain();                android.view.IWindowSession _result;                try {                    _data.writeInterfaceToken(DESCRIPTOR);                    _data.writeStrongBinder((((client!=null))?(client.asBinder()):(null)));                    _data.writeStrongBinder((((inputContext!=null))?(inputContext.asBinder()):(null)));                    mRemote.transact(Stub.TRANSACTION_openSession, _data, _reply, 0);                    _reply.readException();                    _result = android.view.IWindowSession.Stub.asInterface(_reply.readStrongBinder());                }                finally {                    _reply.recycle();                    _data.recycle();                }                return _result;            } // end openSession            ...        } // end Proxy    }//end Stub  ...}

    由上代码可知, 它总共定义了以下三个类:

     1) IWindowManager
     2) IWindowManager.Stub
     3) IWindowManager.Stub.Proxy
    

2. 在服务器端:

      WindowManagerService负责实现服务,它的定义如下:   

public class WindowManagerService extends IWindowManager.Stub        implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs

       很显然,它实现了Stub中定义的方法。

3. 在Client端:

      当获取服务时,通过调用以下函数:
IWindowManager.Stub.asInterface(ServiceManager.getService("window"))

         返回IWindowManager.Stub.Proxy,从上面的代码可知,它就是我们熟悉的BpBinder所做的事,只是通过aidl的方式简化的代码的开发,代码由编译时自动生成。

4. AIDL工作流程

           

浏览人工智能教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值