AIDL解析

AIDL是Android中IPC方式中的一種。

AIDL的使用

創(chuàng)建aidl后綴的文件赂弓,服務(wù)端和客戶端各保存一份探越,包名路徑必須一致

interface IMyAidlInterface {
    void getName();
}

創(chuàng)建遠(yuǎn)端服務(wù)

public class RemoteService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return new ServiceBinder();
    }

    private class ServiceBinder extends IMyAidlInterface.Stub {
        
            @Override
            public void getName() {
                Log.i("RemoteService", "getName");
            }
    }
}

客戶端綁定服務(wù)

public class MainActivity extends AppCompatActivity {

    private IMyAidlInterface myAidlInterface;
    
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            myAidlInterface.getName();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent serviceIntent = new Intent("com.example.application.RemoteService");
        bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
    }
}

AIDL生成的文件

aidl的后綴文件是Android studio的簡化處理,IMyAidlInterface具體實(shí)現(xiàn)

public interface IMyAidlInterface extends android.os.IInterface
{
    public static abstract class Stub extends android.os.Binder implements com.example.application.IMyAidlInterface
    {
        private static final java.lang.String DESCRIPTOR = "com.example.application.IMyAidlInterface";
        public Stub()
        {
            this.attachInterface(this, DESCRIPTOR);
        }
        
        public static com.example.application.IMyAidlInterface asInterface(android.os.IBinder obj)
        {
            if ((obj==null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin!=null)&&(iin instanceof com.example.application.IMyAidlInterface))) {
                return ((com.example.application.IMyAidlInterface)iin);
            }
            return new com.example.application.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
        {
            switch (code)
            {
                case INTERFACE_TRANSACTION:
                {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_getName:
                {
                    data.enforceInterface(DESCRIPTOR);
                    this.getName();
                    reply.writeNoException();
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }
        private static class Proxy implements com.example.application.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;
            }
            @Override public void getName() throws android.os.RemoteException
            {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_getName, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
        }
        static final int TRANSACTION_getName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
    }
    public void getName() throws android.os.RemoteException;
}
  1. IMyAidlInterface繼承android.os.IInterface钞它,包含了一個(gè)靜態(tài)內(nèi)部類Stub拜银。
  2. Stub繼承了android.os.Binder實(shí)現(xiàn)IMyAidlInterface接口殊鞭。
    Stub是本地服務(wù)的抽象類遭垛。
  3. Proxy同樣實(shí)現(xiàn)了接口IMyAidlInterface,是Stub的靜態(tài)內(nèi)部類
    Proxy是遠(yuǎn)端服務(wù)在本地的代理
Stub-proxy的工作流程
  1. RemoteService中的IBinder對(duì)象是Stub的具體實(shí)現(xiàn)操灿,在RemoteService被綁定的時(shí)候返回(onServiceConnected)
  2. 客戶端會(huì)調(diào)用Stub中的靜態(tài)方法Stub.asInterface(IBinder)锯仪,拿到遠(yuǎn)端服務(wù)接口IMyAidlInterface
  3. 在asInterface方法中首先會(huì)判斷Binder是否處在當(dāng)前進(jìn)程,如果是不同進(jìn)程構(gòu)造Proxy返回趾盐,否則返回Stub庶喜。構(gòu)造Proxy時(shí),把mServiceBinder賦值給mRemote救鲤,Proxy中實(shí)現(xiàn)的接口getName會(huì)調(diào)用mRemote的transact方法久窟,而Binder的通信是靠transact和onTransact實(shí)現(xiàn)的,最后會(huì)走到Stub的onTransact本缠,完成對(duì)mServiceBinder的調(diào)用

aidl通信體現(xiàn)著代理模式的設(shè)計(jì)思想斥扛,RemoteService具體實(shí)現(xiàn)了Stub,Proxy是Stub在本地的代理對(duì)象丹锹,Proxy與Stub依靠transact和onTransact通信稀颁,Proxy與Stub的封裝設(shè)計(jì)最終很方便地完成了本地與服務(wù)跨進(jìn)程通信

判斷是否是同個(gè)進(jìn)程

通過Stub.asInterface(iBinder) 判斷Binder是否處在當(dāng)前進(jìn)程

  1. DESCRIPTOR是當(dāng)前類接口描述符,為類的全路徑楣黍,這也是我們?cè)谑褂胊idl 的時(shí)候服務(wù)端和客戶端必須要保證相同路徑下的原因匾灶,因?yàn)樗槐4嫦聛碜鳛閰?shù)用于比對(duì)當(dāng)前類是否是本地類還是遠(yuǎn)程。
  2. 我們調(diào)用IMyAidlInterface.Stub.asInterface 來獲取一個(gè) IMyAidlInterface 類型的類租漂,這個(gè)Stub 是客戶端本地的Stub
  3. 之后會(huì)調(diào)用queryLocalInterface 通過 DESCRIPTOR 獲取接口對(duì)象阶女,這個(gè)方法是用來查詢本地接口的颊糜,有說明是同個(gè)進(jìn)程則直接返回,沒有就返回null秃踩。這個(gè)obj 是binder 代理芭析,因?yàn)槲覀兊姆?wù)在遠(yuǎn)程所以其類型是一個(gè)BinderProxy ,調(diào)用的是BinderProxy 的 queryLocalInterface
public static abstract class Stub extends android.os.Binder implements com.example.application.IMyAidlInterface
    {
        private static final java.lang.String DESCRIPTOR = "com.example.application.IMyAidlInterface";
     
        public static com.example.application.IMyAidlInterface asInterface(android.os.IBinder obj)
        {
            if ((obj==null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin!=null)&&(iin instanceof com.example.application.IMyAidlInterface))) {
                return ((com.example.application.IMyAidlInterface)iin);
            }
            return new com.example.application.IMyAidlInterface.Stub.Proxy(obj);
        }

queryLocalInterface返回null吞瞪,也就是asInterface 中iin為null 所以返回了一個(gè)IMyAidlInterface.Stub.Proxy(obj) 馁启,也就是 IMyAidlInterface 的代理對(duì)象,我們可以通過這個(gè)代理接口調(diào)用遠(yuǎn)程服務(wù)

public final class BinderProxy implements IBinder {
    
    public @Nullable IInterface queryLocalInterface( String descriptor) {
          return null;
    }
}

如果是一個(gè)本地Binder芍秆,執(zhí)行的是Binder 中的queryLocalInterface 方法
這里mDescriptor 就是上邊說的本地Binder 初始化時(shí)保存的接口描述符惯疙,這里兩個(gè)進(jìn)行對(duì)比,如果一樣妖啥,就返回本地接口對(duì)象
然后返回本地久接口實(shí)例霉颠,這樣就不走binder跨進(jìn)程通信 ,走本地通信就可以了。

public class Binder implements IBinder {

    public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) {
        if (mDescriptor != null && mDescriptor.equals(descriptor)) {
            return mOwner;
        }
        return null;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末荆虱,一起剝皮案震驚了整個(gè)濱河市蒿偎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌怀读,老刑警劉巖诉位,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異菜枷,居然都是意外死亡苍糠,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門啤誊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來岳瞭,“玉大人,你說我怎么就攤上這事蚊锹⊥ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵牡昆,是天一觀的道長姚炕。 經(jīng)常有香客問我,道長迁杨,這世上最難降的妖魔是什么钻心? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮铅协,結(jié)果婚禮上捷沸,老公的妹妹穿的比我還像新娘。我一直安慰自己狐史,他們只是感情好痒给,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布说墨。 她就那樣靜靜地躺著,像睡著了一般苍柏。 火紅的嫁衣襯著肌膚如雪尼斧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天试吁,我揣著相機(jī)與錄音棺棵,去河邊找鬼。 笑死熄捍,一個(gè)胖子當(dāng)著我的面吹牛烛恤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播余耽,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼缚柏,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了碟贾?” 一聲冷哼從身側(cè)響起币喧,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎袱耽,沒想到半個(gè)月后杀餐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扛邑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年怜浅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蔬崩。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖搀暑,靈堂內(nèi)的尸體忽然破棺而出沥阳,到底是詐尸還是另有隱情,我是刑警寧澤自点,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布桐罕,位于F島的核電站,受9級(jí)特大地震影響桂敛,放射性物質(zhì)發(fā)生泄漏功炮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一术唬、第九天 我趴在偏房一處隱蔽的房頂上張望薪伏。 院中可真熱鬧,春花似錦粗仓、人聲如沸嫁怀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽塘淑。三九已至萝招,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間存捺,已是汗流浹背槐沼。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留捌治,地道東北人母赵。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像具滴,于是被迫代替她去往敵國和親凹嘲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容