Android Binder機制總結

知識要點:

1. Binder通信機制的架構

2. 實現進程間通信 的步驟

3. 完整的Binder通信過程

4. Binder驅動的內核 空間和用戶空間的數據傳輸

5. AIDL工具的原理

6. Binder通信過程中進程和 線程的切換,什麼是Binder線程

應用實現進程間通信的類圖

應用發起進程間通信調用的流程圖

AIDL文件編譯生成後的java文件

package com.xiaopeng.montecarlo.root;
// Declare any non-default types here with import statements

public interface IMyAidlInterface extends android.os.IInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    public void interfaceA(int type) throws android.os.RemoteException;

    public void interfaceB(int type) throws android.os.RemoteException;

    /**
     * Local-side IPC implementation stub class.
     * 表示服務端的內部類Stub
     */
    public static abstract class Stub extends android.os.Binder implements com.xiaopeng.montecarlo.root.IMyAidlInterface {
        static final int TRANSACTION_interfaceA = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_interfaceB = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
        private static final java.lang.String DESCRIPTOR = "com.xiaopeng.montecarlo.root.IMyAidlInterface";

        /**
         * Construct the stub at attach it to the interface.
         */
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        /**
         * Cast an IBinder object into an com.xiaopeng.montecarlo.root.IMyAidlInterface interface,
         * generating a proxy if needed.
         */
        public static com.xiaopeng.montecarlo.root.IMyAidlInterface asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.xiaopeng.montecarlo.root.IMyAidlInterface))) {
                return ((com.xiaopeng.montecarlo.root.IMyAidlInterface) iin);
            }
            return new com.xiaopeng.montecarlo.root.IMyAidlInterface.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 {
            java.lang.String descriptor = DESCRIPTOR;
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(descriptor);
                    return true;
                }
                case TRANSACTION_interfaceA: {
                    data.enforceInterface(descriptor);
                    int _arg0;
                    _arg0 = data.readInt();
                    this.interfaceA(_arg0);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_interfaceB: {
                    data.enforceInterface(descriptor);
                    int _arg0;
                    _arg0 = data.readInt();
                    this.interfaceB(_arg0);
                    reply.writeNoException();
                    return true;
                }
                default: {
                    return super.onTransact(code, data, reply, flags);
                }
            }
        }

        //表示客戶端的內部類Proxy
        private static class Proxy implements com.xiaopeng.montecarlo.root.IMyAidlInterface {
            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;
            }

            /**
             * Demonstrates some basic types that you can use as parameters
             * and return values in AIDL.
             */
            @Override
            public void interfaceA(int type) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeInt(type);
                    mRemote.transact(Stub.TRANSACTION_interfaceA, _data, _reply, 0);
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }

            @Override
            public void interfaceB(int type) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeInt(type);
                    mRemote.transact(Stub.TRANSACTION_interfaceB, _data, _reply, 0);
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
        }
    }
}

推薦資料:
Android Binder 

Android Binder 分析——內存管理【轉】

深入理解Android內核思想

簡單理解Android Binder通信(AIDL)

Android之bindService流程

Binder死亡通知機制之linkToDeath

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章