Android AIDL分析

來源:互聯網
上載者:User
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工作流程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.