日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

解析Android AIDL的實例與原理

瀏覽:45日期:2022-09-17 16:23:52
目錄一、概述二、創建 .aidl 文件三、生成 .java 文件四、傳輸復雜數據五、建立 service六、獲取服務七、分析調用過程一、概述

簡單來說,AIDL 就是定義一個接口,客戶端(調用端)通過 bindService 來與遠程服務端建立一個連接,在該連接建立時會將返回一個 IBinder 對象,該對象是服務端 Binder 的 BinderProxy。在建立連接時,客戶端通過 asInterface 函數將該 BinderProxy 對象包裝成本地的 Proxy,并賦值給Proxy類的 mRemote 字段,本地通過 mRemote 即可調用遠程方法。

二、創建 .aidl 文件

首先打開 Android Studio,new 一個 AIDL file。具體代碼如下 :

interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);}

basicTypes 方法事接口自帶的,不過可以知道,在 aidl 中只能使用這些基本類型參數:int, long, boolean, float,double, String ;

除了basicTypes 方法之外,我們也可以添加自己的方法。因此,可以刪除basicTypes 方法,添加自己的方法。

三、生成 .java 文件

添加完方法之后,選中 .aidl 文件,在彈出的菜單中選擇 Synchronize LocalAIDLS... Service.java,就會會自動幫你生成對應的 java 代碼。

格式化代碼之后,如下所示:

package com.example.databasetest;public interface IMyAidlInterface extends android.os.IInterface { /** * Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.example.databasetest.IMyAidlInterface {private static final java.lang.String DESCRIPTOR = 'com.example.databasetest.IMyAidlInterface';/** * Construct the stub at attach it to the interface. */public Stub() { this.attachInterface(this, DESCRIPTOR);}/** * Cast an IBinder object into an com.example.databasetest.IMyAidlInterface interface, * generating a proxy if needed. */public static com.example.databasetest.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.databasetest.IMyAidlInterface))) {return ((com.example.databasetest.IMyAidlInterface) iin); } return new com.example.databasetest.IMyAidlInterface.Stub.Proxy(obj);}@Overridepublic android.os.IBinder asBinder() { return this;}@Overridepublic 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_basicTypes: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); long _arg1; _arg1 = data.readLong(); boolean _arg2; _arg2 = (0 != data.readInt()); float _arg3; _arg3 = data.readFloat(); double _arg4; _arg4 = data.readDouble(); java.lang.String _arg5; _arg5 = data.readString(); this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5); reply.writeNoException(); return true;} } return super.onTransact(code, data, reply, flags);}private static class Proxy implements com.example.databasetest.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 basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) 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(anInt); _data.writeLong(aLong); _data.writeInt(((aBoolean) ? (1) : (0))); _data.writeFloat(aFloat); _data.writeDouble(aDouble); _data.writeString(aString);// 這里是重點,proxy 持有引用,這樣就可以進行數據交換,也不會暴露這個對象 mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0); _reply.readException();} finally { _reply.recycle(); _data.recycle();} }}static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;}

如果,你需要修改 .aidl 文件,那么修改之后,選擇 build -> make project 即可,會重新生成對應的java文件。

對于生成的這個java 類,很多剛接觸的人會不理解,這里需要解釋下:

IMyAidlInterface :這個是我們自己定義的一個 servier 接口,也就是將你想要有的功能定義在接口中; IBinder:定義了與遠程對象的交互協議,代表一種跨進程傳輸的能力,實現這個接口,就能將這個對象進行跨進程傳遞,但是如果要使用的話,推薦繼承其子類 Binder; Binder:實現了 IBinder 接口,代表的其實就是Binder 本地對象。BinderProxy 類是 Binder 類的一個內部類,它代表遠程進程的 Binder 對象的本地代理;這兩個類都繼承自IBinder, 因而都具有跨進程傳輸的能力;實際上,在跨越進程的時候,Binder 驅動會自動完成這兩個對象的轉換。 Stub: AIDL 的時候,編譯工具會給我們生成一個名為 Stub 的靜態內部抽象類;這個類繼承了 Binder, 說明它是一個 Binder 本地對象,它實現了 IInterface 接口,表明它具有 Server 承諾給 Client 的能力;Stub 是一個抽象類,具體的 IInterface 的相關實現需要開發者自己實現。 IInterface:IInterface 代表的就是 Server 進程對象具備什么樣的能力(能提供哪些方法,其實對應的就是 AIDL 文件中定義的接口) proxy:Stub 的靜態內部類,是一個實現了IMyAidlInterface接口,所以他是一個遠程代理對象,可以用于返回給客戶端用。當 client 調用 proxy的某個方法的時候,會將參數傳到 proxy 中,在通過其持有的遠程實際對象,將方法名和參數等都傳給遠程實際對象,然后就會回調onTransact,對應的方法就會被調用,以此來實現跨進程調用。四、傳輸復雜數據

如果,需要傳遞復雜數據,那么就需要實現Parcelable 接口,可序列化:

public class Info implements Parcelable { private String content; public String getContent() {return content; } public void setContent(String content) {this.content = content; } public Info() { } public Info(Parcel in) {content = in.readString(); } public static final Creator<Info> CREATOR = new Creator<Info>() {@Overridepublic Info createFromParcel(Parcel in) { return new Info(in);}@Overridepublic Info[] newArray(int size) { return new Info[size];} }; @Override public int describeContents() {return 0; } @Override public void writeToParcel(Parcel dest, int flags) {dest.writeString(content); } /** * 參數是一個Parcel,用它來存儲與傳輸數據 * * @param dest */ public void readFromParcel(Parcel dest) {//注意,此處的讀值順序應當是和writeToParcel()方法中一致的content = dest.readString(); } //方便打印數據 @Override public String toString() {return 'content : ' + content; }}

與此同時,也要建一個 info.aidl 文件,表明數據也是可以傳遞的。

package com.viii.aidlclient;//注意:Info.Info.java的包名應當是一樣的//這個文件的作用是引入了一個序列化對象 Info 供其他的AIDL文件使用//注意parcelable是小寫parcelable Info;

這樣就可以使用 info 對象了。 不用在受前面的基本類型變量所控制。

五、建立 service

接下去,新建一個Service負責接收消息,并在AndroidManifest.xml里面注冊 Service:

public class MyService extends Service { private static final String TAG = 'MyService'; // private MyBinder mMyBinder = new MyBinder(); @Nullable @Override public IBinder onBind(Intent intent) {Log.d(TAG, 'onBind: '); // 應該返回 mBinderreturn null; } @Override public void onCreate() {Log.d(TAG, 'onCreate: ');super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, 'onStartCommand: ');return super.onStartCommand(intent, flags, startId); }// 這里就是服務端的實現,繼承了 stub,想要怎么樣的能力,自己去實現 private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {@Overridepublic void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {// 具體實現過程} };}

這時候,可以basicTypes 方法添加具體函數代碼,實現你想要的功能。

當我們在本地獲取到代理后之后,調用basicTypes 就會觸發服務端的調用。

六、獲取服務

接下去在 mainactivity 中進行綁定。

public class MainActivity extends AppCompatActivity { private static final String TAG = 'MainActivity'; private IMyAidlInterface mService; private boolean mIsBound; private AdditionServiceConnection mServiceConnection; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);doBindService() ; }/** * bind service */ private void doBindService() {mServiceConnection = new AdditionServiceConnection();Intent intent = new Intent(this, MyService.class);bindService(intent, mServiceConnection, BIND_AUTO_CREATE); } /** * unbind service */ private void doUnbindService() {if (mIsBound) { unbindService(mServiceConnection); mServiceConnection = null; mIsBound = false;} } /** * ServiceConection */ class AdditionServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { // 連接的時候獲取本地代理,這樣我們就可以調用 service 中的方法了。 mService = IMyAidlInterface.Stub.asInterface((IBinder) service); mIsBound = true; try {//設置死亡代理service.linkToDeath(mDeathRecipient, 0); } catch (RemoteException e) {e.printStackTrace(); } Log.d(TAG, 'onServiceConnected: ');}@Overridepublic void onServiceDisconnected(ComponentName name) { mService = null; mIsBound = false; Log.d(TAG, 'onServiceDisconnected: ');} } /** * 監聽Binder是否死亡 */ private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {@Overridepublic void binderDied() { if (mService == null) {return; } mService.asBinder().unlinkToDeath(mDeathRecipient, 0); mService = null; //重新綁定 doBindService();} }; @Override protected void onStop() {super.onStop();doUnbindService(); }}

將遠程服務的 binder 拿到之后,我們就可以調用相關方法實現自己的功能呢。

到這里,一個 AIDL 就被我們實現了。

七、分析調用過程

看看 asInterface 方法,我們在 bind 一個 Service 之后,在 onServiceConnecttion 的回調里面,就是通過這個方法拿到一個遠程的 service 的,這個方法做了什么呢?

/** * Cast an IBinder object into an com.example.databasetest.IMyAidlInterface interface, * generating a proxy if needed. */public static com.example.databasetest.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.databasetest.IMyAidlInterface))) {return ((com.example.databasetest.IMyAidlInterface) iin); }  // 實際上,代理對象持有真實對象,同時代理對象會對數據進行處理后,再調用實體對象的方法 return new com.example.databasetest.IMyAidlInterface.Stub.Proxy(obj);}

首先看函數的參數IBinder類型的 obj,這個對象是驅動給我們的,如果是 Binder 本地對象,那么它就是 Binder 類型,如果是 Binder 代理對象,那就是BinderProxy類型;它會試著查找 Binder 本地對象,如果找到,說明 Client 和 Server 都在同一個進程,這個參數直接就是本地對象,直接強制類型轉換然后返回。

如果找不到,說明是遠程對象(處于另外一個進程)那么就需要創建一個 Binder 代理對象,讓這個 Binder 代理實現對于遠程對象的訪問。一般來說,如果是與一個遠程 Service 對象進行通信,那么這里返回的一定是一個 Binder 代理對象,這個 IBinder 參數的實際上是 BinderProxy;

再看看我們對于 aidl 的basicTypes方法的實現;在 Stub 類里面,basicTypes是一個抽象方法,我們需要繼承這個類并實現它;如果 Client 和 Server 在同一個進程,那么直接就是調用這個方法;那么,如果是遠程調用,這中間發生了什么呢?Client 是如何調用到 Server 的方法的?

對于遠程方法的調用,是通過 Binder 代理完成的,在這個例子里面就是Proxy類;Proxy對于basicTypes方法的實現如下:

public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) 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(anInt);_data.writeLong(aLong);_data.writeInt(((aBoolean) ? (1) : (0)));_data.writeFloat(aFloat);_data.writeDouble(aDouble);_data.writeString(aString);// 這里是重點,調用的是實體對象的方法mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);_reply.readException(); } finally {_reply.recycle();_data.recycle(); }}

它首先用 Parcel 把數據序列化了,然后調用了 transact 方法;這個 transact 到底做了什么呢?這個 Proxy 類在 asInterface 方法里面被創建,前面提到過,如果是 Binder 代理那么說明驅動返回的 IBinder 實際是 BinderProxy,因此我們的 Proxy 類里面的 mRemote 實際類型應該是BinderProxy;我們看看 BinderProxy 的 transact 方法:( Binder.java 的內部類)

public native boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException;

這是一個本地方法;它的實現在 native 層,具體來說在 frameworks/base/core/jni/android_util_Binder.cpp 文件,里面進行了一系列的函數調用,調用鏈實在太長這里就不給出了;要知道的是它最終調用到了talkWithDriver函數;看這個函數的名字就知道,通信過程要交給驅動完成了;這個函數最后通過 ioctl 系統調用,Client 進程陷入內核態,Client 調用 basicTypes 方法的線程掛起等待返回;驅動完成一系列的操作之后喚醒 Server 進程,調用了Server進程本地對象的 onTransact 函數(實際上由 Server 端線程池完成)。我們再看 Binder 本地對象的 onTransact 方法(這里就是 Stub 類里面的此方法):

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_basicTypes: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); long _arg1; _arg1 = data.readLong(); boolean _arg2; _arg2 = (0 != data.readInt()); float _arg3; _arg3 = data.readFloat(); double _arg4; _arg4 = data.readDouble(); java.lang.String _arg5; _arg5 = data.readString(); this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5); reply.writeNoException(); return true;} } return super.onTransact(code, data, reply, flags);}

在 Server 進程里面,onTransact 根據調用號(每個 AIDL 函數都有一個編號,在跨進程的時候,不會傳遞函數,而是傳遞編號指明調用哪個函數)調用相關函數。

在這個例子里面,調用了 Binder 本地對象的 basicTypes 方法;這個方法將結果返回給驅動,驅動喚醒掛起的 Client 進程里面的線程并將結果返回。于是一次跨進程調用就完成了。

至此,你應該對 AIDL 這種通信方式里面的各個類以及各個角色有了一定的了解;它總是那么一種固定的模式:一個需要跨進程傳遞的對象一定繼承自 IBinder,如果是 Binder 本地對象,那么一定繼承 Binder 實現 IInterface,如果是代理對象,那么就實現了 IInterface 并持有了 IBinder 引用;

Proxy 與 Stub 不一樣,雖然他們都既是 Binder 又是 IInterface,不同的是 Stub 采用的是繼承(is 關系),Proxy采用的是組合(has 關系)。他們均實現了所有的 IInterface 函數。

不同的是,Stub又使用策略模式調用的是虛函數(待子類實現),而 Proxy 則使用組合模式。為什么Stub采用繼承而 Proxy 采用組合?事實上,Stub 本身 is 一個 IBinder(Binder),它本身就是一個能跨越進程邊界傳輸的對象,所以它得繼承 IBinder 實現 transact 這個函數從而得到跨越進程的能力(這個能力由驅動賦予)。

Proxy 類使用組合,是因為他不關心自己是什么,它也不需要跨越進程傳輸,它只需要擁有這個能力即可,要擁有這個能力,只需要保留一個對 IBinder 的引用。

以上就是解析Android AIDL的實例與原理的詳細內容,更多關于Android AIDL的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲日本免费电影| 欧美a级片一区| 亚洲精品三级| 日本一区中文字幕| 日韩精品中文字幕一区二区| 色8久久久久| 日韩av三区| 国产精品亚洲一区二区在线观看| 欧美日韩一区二区高清| 国产精品香蕉| 色欧美自拍视频| 色综合www| 日韩欧美1区| 亚洲黄页一区| 日韩成人一级| 精品久久美女| 在线日韩视频| 一区二区三区网站| 欧美一级二级三级视频| 国产精品久久久久久久久久白浆 | 久久久精品区| 亚洲一级少妇| 免费不卡中文字幕在线| 欧美午夜不卡| 免播放器亚洲| 日韩动漫一区| 精品美女在线视频| 欧美日韩一区二区综合| 亚洲综合二区| 国产精品香蕉| 88xx成人免费观看视频库| 欧美日韩在线二区| 亚洲综合五月| 激情综合五月| 亚洲国产一区二区三区在线播放| 蜜桃视频在线观看一区二区| 国产精区一区二区| 久久亚洲专区| 日韩国产欧美三级| 成人午夜在线| 香蕉久久夜色精品国产| 国产精品极品在线观看| 欧美不卡高清一区二区三区| 日韩中文欧美在线| 精品资源在线| 中文国产一区| 老司机精品在线| 国产日产高清欧美一区二区三区| 国产乱论精品| 欧美片网站免费| 国产精品成人一区二区不卡| 美女久久久久| 国产精品日韩精品中文字幕| 久久婷婷亚洲| 爽好久久久欧美精品| 国产精品激情电影| 五月婷婷亚洲| 国产精品一级| 国产免费成人| 国内不卡的一区二区三区中文字幕| 亚洲二区在线| 国产精品白浆| 天堂av在线一区| 高清一区二区三区av| 亚洲精品在线二区| 激情久久五月| 欧美精品二区| 亚州av一区| 亚洲国内精品| 国产亚洲电影| 日韩一级精品| 国产精品精品| 国产欧美日韩精品一区二区三区| 黑丝美女一区二区| 精品淫伦v久久水蜜桃| 日韩中文字幕不卡| 久久国产日韩| 电影91久久久| 蜜桃久久久久久| 日韩在线高清| 麻豆国产欧美日韩综合精品二区| 视频一区中文| 91亚洲国产成人久久精品| 日韩精品免费观看视频| 国产aa精品| 国产亚洲精品精品国产亚洲综合| 亚洲综合精品四区| 啪啪国产精品| 欧美国产一级| 国产极品嫩模在线观看91精品| 视频一区二区国产| 亚洲精品国产偷自在线观看| 国产欧洲在线| 久久精品国产99国产| 91免费精品国偷自产在线在线| 一区二区三区网站| 亚洲在线一区| 91久久久久| 国产91久久精品一区二区| 国产精品1区| 日本亚洲三级在线| 亚洲神马久久| 亚洲国产一区二区三区在线播放 | 亚洲久久一区| 久久av一区二区三区| 亚洲欧美一区在线| 1000部精品久久久久久久久| 午夜精品久久久久久久久久蜜桃| 久久精品国产久精国产| 国产精品一站二站| 悠悠资源网久久精品| 午夜视频精品| 欧美精品不卡| 欧美一区精品| 日韩精品一区二区三区中文| 美女黄网久久| 91久久视频| 国产亚洲在线观看| 亚洲免费网址| 亚洲一区二区日韩| 亚洲综合婷婷| 午夜天堂精品久久久久| 亚洲精品九九| 18国产精品| 国产精品大片| 精品一区电影| 国产中文在线播放| 久久婷婷av| 成人羞羞视频在线看网址| 91精品综合| 99成人在线| 亚洲一区av| 欧美亚洲人成在线| 麻豆精品视频在线观看免费| 丰满少妇一区| 成人日韩在线| 欧洲激情综合| 石原莉奈一区二区三区在线观看 | 久久国产亚洲精品| 不卡一区综合视频| 水蜜桃久久夜色精品一区的特点| 日韩精品一级二级| 亚州av日韩av| 国产一区二区亚洲| 不卡一区2区| 亚洲综合欧美| 亚洲精品少妇| 国产日产一区| 美女久久精品| 91av亚洲| 中文精品视频| 热久久久久久| 国产成人免费| 亚洲欧美日韩高清在线| 综合干狼人综合首页| 欧美日韩在线精品一区二区三区激情综合 | 99久久精品网| 蜜桃久久av| 国产欧美在线观看免费| 国产精品国产一区| 亚洲精品小说| 日韩av一二三| 在线看片国产福利你懂的| 黄色成人91| 国产日韩欧美| 久久久777| 日韩和欧美的一区| yellow在线观看网址| 久久精品国产68国产精品亚洲| 奶水喷射视频一区| 国产欧美日韩亚洲一区二区三区| 日韩黄色大片| 亚洲一区av| 国产成人精品一区二区三区在线| 在线国产一区二区| 国产精品欧美三级在线观看| 日韩欧美一区二区三区在线观看| 国产在线视频欧美一区| 日韩一级不卡| 欧美a一区二区| 久久裸体视频| 青青在线精品| 999久久久精品国产| 久久成人精品| 久久在线91| 视频一区二区三区入口| 免费一级欧美在线观看视频 | 国产成人免费av一区二区午夜| 日韩视频一区二区三区在线播放免费观看| 欧美一级网址| 欧美精选一区二区三区| 国产精品白浆| 亚洲永久字幕| 日本精品黄色| 日韩极品在线观看| 天堂网在线观看国产精品| 国产精品资源| 欧美日韩激情在线一区二区三区| 国产精品一区二区中文字幕| 红桃视频欧美|