Android AIDL的实现
2019獨角獸企業重金招聘Python工程師標準>>>
?? ? ?AIDL (Android Interface Definition Language) 是一種IDL 語言,用于生成可以在Android設備上兩個進程之間進行進程間通信(interprocess communication, IPC)的代碼。如果在一個進程中(例如Activity)要調用另一個進程中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。
【一】AIDL文件的創建:
?? ? AIDL使用簡單的語法來聲明接口,描述其方法以及方法的參數和返回值。這些參數和返回值可以是任何類型,甚至是其他AIDL生成的接口。
?? ? 其中對于Java編程語言的基本數據類型 (int, long, char, boolean等),String和CharSequence,集合接口類型List和Map,不需要import 語句。?而如果需要在AIDL中使用其他AIDL接口類型,需要import,即使是在相同包結構下。AIDL允許傳遞實現Parcelable接口的類,需要import.? ?? ? 需要特別注意的是,?對于非基本數據類型,也不是String和CharSequence類型的,需要有方向指示,包括in、out和inout,in表示由客戶端設置,out表示由服務端設置,inout是兩者均可設置。?? ?AIDL只支持接口方法,不能公開static變量。
?? ?其中AIDL的方法還提供了oneway這個關鍵字,可以用關鍵字oneway來標明遠程調用的行為屬性,使用了該關鍵字,那么遠程調用將僅僅是調用所需的數據傳輸過來并立即返回,而不會等待結果的返回,也即是說不會阻塞遠程線程的運行。AIDL接口將最終將獲得一個從Binder線程池中產生的調用(和普通的遠程調用類似)。如果關鍵字oneway在本地調用中被使用,將不會對函數調用有任何影響。 ?
?? ?1.服務端接口的創建:
package com.liusl.aidl; import com.liusl.aidl.AIDLCallback; interface ServiceAIDL {void registerClient(AIDLCallback cb);void unregisterClient(AIDLCallback cb);int add(int i, int j);int sum(in int[] numbers); }?? ?2.回調接口的創建:
package com.liusl.aidl;interface AIDLCallback {int returnResult(int result); }【二】服務端接口的實現:
????將AIDLCallback作為服務端給客戶端的回調,這樣才實現了服務端和客戶端的通信。同時使服務端繼承service類,實現onBind方法,創建AIDLService對象,這樣才能將服務端的接口提供給客戶端進行調用。
package com.liusl.aidl;import com.liusl.aidl.AIDLCallback; import com.liusl.aidl.ServiceAIDL; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log;public class MyAIDLService extends Service {public final String TAG = "[service]";int sum = 0;@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "[MyAIDLService] onBind(intent)...");return (IBinder) new ServiceAIDLImpl();}class ServiceAIDLImpl extends ServiceAIDL.Stub {private AIDLCallback cb;@Overridepublic void registerClient(AIDLCallback cb) throws RemoteException { this.cb = cb;Log.i(TAG, "[ServiceAIDLImpl] registerClient start...");cb.asBinder().linkToDeath(new DeathRecipient() {@Overridepublic void binderDied() {try {Log.i(TAG, "[ServiceAIDLImpl]binderDied.");} catch (Throwable e) {}}}, 0);Log.i(TAG, "[ServiceAIDLImpl] registerClient end...");}@Overridepublic void unregisterClient(AIDLCallback cb) throws RemoteException {}@Overridepublic int add(int i, int j) throws RemoteException {sum = 0;sum = i + j;cb.returnResult(sum);return 0;}@Overridepublic int sum(int[] numbers) throws RemoteException {sum = 0;for (int i = 0; i < numbers.length; i++) {sum += numbers[i];}cb.returnResult(sum);return 0;}} }
【三】服務的創建及Activity的實現:
?? ? ? ? 在客戶端使用服務端的AIDL前,需要先通過創建一個客戶端與服務端的連接,步驟如下:
?? ?1.創建intent:
????????String actionName = "com.liusl.aidl.MyAIDLService";
????????Intent intent = new Intent(actionName)
?? ?2.實現一個MyServiceConnection類,繼承ServiceConnection,重寫onServiceConnected和onServiceDisConnected方法。
?? ?3.新建了BindService對象:
????????MyServiceConnection connection = new MyServiceConnection();
????????通過調用bindService(intent, connection, Context.BIND_AUTO_CREATE);->onServiceConnected(),在OnServiceConnected()中獲取服務端得aidl對象,
????????myservice = (ServiceAIDL) ServiceAIDL.Stub.asInterface(service);
?? ?4.實現客戶端的AIDLCallback接口:
private class AIDLCallbackImpl extends AIDLCallback.Stub {public int returnResult(int result) {textView.setText("Result :" + result);return result;}}?? ?5.通過服務端的aidl對象調用服務端的注冊方法以及其他的方法:
????myservice.registerClient(callBack);
????myservice.add(2, 3);
????由此就完成了客戶端注冊一個客戶到服務端并且調用服務端的add方法,服務端將add的結果反饋給回調方法,客戶端在接收到回調傳來的值后進行顯示操作。
注意:需要在AndroidMainFest.xml中對服務進行注冊:
<service android:name="com.liusl.aidl.MyAIDLService"><intent-filter><action android:name="com.liusl.aidl.MyAIDLService"/></intent-filter></service>
代碼所在位置:
http://www.oschina.net/code/snippet_1051613_22116
轉載于:https://my.oschina.net/hiliusl/blog/137384
總結
以上是生活随笔為你收集整理的Android AIDL的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NSInteger与int的区别
- 下一篇: Android NDK: WARNING