Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

深入解析 Android 介面定義語言 AIDL

編輯:關於 Android 程式設計

AIDL(Android Interface Definition Language)與其他介面定義語言類似,讓 client 與 service 能透過定義好的介面進行跨進程通訊。由於物件要能在進程間傳遞,必須先拆解成系統能識別的指令,手動實作這部分既冗長又枯燥,而 Android 透過 AIDL 就能自動處理。

注意:只有在需要讓不同應用存取 service,且 service 必須處理多執行緒時,才需要使用 AIDL。若只是本地通訊、不需處理 IPC,實作 Binder 即可;若需要 IPC 但不必處理多執行緒,則可使用 Messenger。使用前請先評估哪一種方式最適合自己。

設計前的考量:執行緒模型

設計 AIDL 介面前,必須先確認呼叫發生時的執行緒行為,這取決於呼叫是來自本地進程還是遠程進程:

本地進程的呼叫:在同一個執行緒中執行。若是主 UI 執行緒,會繼續在 AIDL 介面上執行;若是其他執行緒,則在 service 中執行其程式碼。若僅由本地執行緒存取,其實可用 Binder 取代 AIDL。

遠程進程的呼叫:會從執行緒池中發起,因此必須做好同時有多個未知執行緒呼叫的準備,也就是說,AIDL 的實作必須保證執行緒安全。

oneway 關鍵字會改變遠程呼叫的行為:使用後,遠程呼叫不會阻塞,它只會送出交易資料並立即返回,實作端最終仍會透過 Binder 執行緒池收到這個呼叫。若 oneway 用於本地呼叫則無影響,呼叫仍是同步的。

定義 AIDL 介面

可用 Java 語法在 src/ 下定義 AIDL 介面。當建置時,Android SDK 工具會依 .aidl 檔案產生一個 IBinder 介面,存放於專案的 gen/ 目錄。service 需實作該介面,client 則可綁定 service 並呼叫其中的方法。

AIDL 支援的資料型別包括:所有 Java 基本型別(int、long、char、boolean 等)、String、CharSequence、List 與 Map。其中 List 與 Map 的元素必須是 AIDL 支援的型別、其他 AIDL 介面或已定義的 parcelable;實作時具體類別分別為 ArrayList 與 HashMap。若要使用未列出的型別,即使位於同一個 package,也必須 import。

定義介面時需留意:方法可有零個或多個參數,並可回傳值或 void;所有非基本型別的參數都需標註方向 in、out 或 inout(基本型別預設為 in 且不可更改)。由於資料轉換成本高昂,務必審慎考量真正需要的資料方向。此外,AIDL 僅支援方法,不支援靜態成員變數。

以下是一個 .aidl 檔案範例:

// IRemoteService.aidl
package com.example.android;

/** Example service interface */
interface IRemoteService {
/** Request the process ID of this service, to do evil things with it. */
int getPid();

/** 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);
}

實作介面

建置專案後,SDK 工具會產生含 Stub 抽象內部類別的 Java 介面。要實作它,只需繼承 Stub 並完成各方法即可。Stub 也定義了輔助方法,其中最關鍵的是 asInterface(),它能將傳入的 IBinder 轉型為 Stub 介面的實例。

private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
// Does nothing
}
};

實作時需遵守幾點規則:呼叫不一定來自主執行緒,因此務必確保執行緒安全;遠程呼叫預設為同步,若 service 任務耗時,不應從 Activity 主執行緒呼叫,否則會導致應用掛起;此外,service 不會把開發者自行拋出的例外傳回呼叫方。

將介面公開給客戶端

service 實作介面後,需繼承 Service 並重寫 onBind(),回傳 Stub 實例:

public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
}
};
}

當 client 呼叫 bindService() 綁定後,會透過 onServiceConnected() 回調取得 mBinder 實例。若 client 與 service 位於不同應用,client 必須把 .aidl 檔案複製到自己的 src/ 目錄,然後呼叫 YourServiceInterface.Stub.asInterface(service) 完成轉型:

IRemoteService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mIRemoteService = IRemoteService.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName className) {
mIRemoteService = null;
}
};

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved