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

Android AIDL 跨進程通訊入門與實作

編輯:關於 Android 程式設計

AIDL(Android Interface Definition Language)是 Android 實現進程間通訊(IPC)的重要機制,特別適合 Service 端存在多執行緒並發的情境。如果只是單執行緒,用 Messenger 即可;若完全不需要 IPC,則用 Binder 就足夠。使用 AIDL 時,基本資料型別都支援,List、Map 等有限支援,而 static 欄位則不適用。下面按步驟說明它的用法。

1. 撰寫 .aidl 檔案

介面描述檔案需要包含:匯入的套件名、方法定義;若使用自訂物件,該物件需實作 Parcelable 介面,並匯入對應的套件名加類名(基本型別則不需要)。此外,所有 .aidl 檔案以及需要傳遞的物件,都必須在 Service 與 Client 中各放一份。

package com.aidl;
import com.aidl.Data;
interface IaidlData {
    int getPid();
    String getName();
    com.aidl.Data getData();
}

2. 在 Service 中實作 AIDL 介面

實際要實作的,是 gen 目錄中自動生成的 IaidlData.java 裡那個抽象內部類 Stub。它繼承自 android.os.Binder 並實作了 IaidlData 介面。同時別忘了在 AndroidManifest.xml 中註冊 Service,並加上 action 屬性(一般使用 aidl 的套件名加檔名,以便服務端與客戶端一致)。

public class AidlService extends Service {
    private final IaidlData.Stub mBinder = new IaidlData.Stub() {
        public int getPid() {
            return Process.myPid();
        }
        public String getName() throws RemoteException {
            return "go or not go is a problem";
        }
        public Data getData() throws RemoteException {
            Data data = new Data();
            data.id = Process.myPid();
            data.name = "go or not go is a problem";
            return data;
        }
    };
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

3. 綁定 Service 並取得介面物件

客戶端透過 action(採用 aidl 檔案的類名加套件名,需與服務端一致)定位 Service,並在 onServiceConnected 回調中取得 Stub 物件:

Intent intent = new Intent(IaidlData.class.getName());
bindService(intent, mSecondaryConnection, BIND_AUTO_CREATE);

private ServiceConnection mSecondaryConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mIaidlData = IaidlData.Stub.asInterface(service);
    }
    public void onServiceDisconnected(ComponentName className) {
        mIaidlData = null;
    }
};

取得介面物件後,就能直接呼叫其中的方法;記得在 onDestroy 中呼叫 unbindService 解除綁定。

4. 傳遞自訂物件所需的 .aidl 檔案

若需在 aidl 中傳遞自訂物件,除了讓該物件實作 Parcelable,還要額外新增對應的 Data.aidl 檔案,並在引用它的 aidl 檔案中 import。服務端與客戶端都需加入一致的 Data.aidl 與 Data.java。

package com.aidl;
parcelable Data;

5. 實作 Parcelable 介面的物件類

最後,讓 Data 類實作 Parcelable,完成讀寫 Parcel 的方法:

public class Data implements Parcelable {
    public int id;
    public String name;
    public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() {
        public Data createFromParcel(Parcel in) {
            return new Data(in);
        }
        public Data[] newArray(int size) {
            return new Data[size];
        }
    };
    private Data(Parcel in) {
        readFromParcel(in);
    }
    public void readFromParcel(Parcel in) {
        id = in.readInt();
        name = in.readString();
    }
    public int describeContents() {
        return 0;
    }
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved