編輯:進階開發
想設計出結構良好的 Android 軟體,AIDL 與 IPC 機制是必須熟悉的題目。掌握它之後,你寫的元件就能像 Windows 的 ActiveX COM 那樣被跨行程重用,也能實作出類似 Symbian 的服務架構——特別適合需要長期在背景執行、沒有 UI 的服務。以下以一個修改自 SDK 文件的範例說明。
建立 android123.aidl:
package cn.com.android123;
import cn.com.android123.IAtmService;
// 宣告介面,此處以銀行 ATM 程式為例
interface IBankAccountService {
int getAccountBalance(); // 回傳整數,無參數
void setOwnerNames(in List<String> names); // 無回傳,含傳入 List 參數
BankAccount createAccount(in String name, int startingDeposit, in IAtmService atmService); // 回傳自訂型別
int getCustomerList(in String branch, out String[] customerList); // 輸入分行,輸出客戶清單
}
Service 端需要覆寫 onBind(),依 action 回傳對應的 Binder:
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
if (IRemoteService.class.getName().equals(intent.getAction())) {
return mBinder;
}
if (ISecondary.class.getName().equals(intent.getAction())) {
return mSecondaryBinder;
}
return null;
}
接著實作 AIDL 編譯器產生的 Stub:
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public void registerCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.register(cb);
}
public void unregisterCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.unregister(cb);
}
};
另一個介面同理:
private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
public int getPid() { return Process.myPid(); }
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) { }
};
跨行程傳遞資料要透過 android.os 提供的 Parcelable。實務上多數人會用 Eclipse + ADT 外掛來產生:在 Package Explorer 檢視中對類別按右鍵,選擇 Create Aidl preprocess file for Parcelable classes,ADT 就會自動補上必要的 aidl 前置處理檔。
以 Rect 為例,實作 Parcelable 的關鍵在於 writeToParcel 與 readFromParcel 的成對處理:
public final class Rect implements Parcelable {
public int left, top, right, bottom;
public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() {
public Rect createFromParcel(Parcel in) { return new Rect(in); }
public Rect[] newArray(int size) { return new Rect[size]; }
};
public void writeToParcel(Parcel out) {
out.writeInt(left); out.writeInt(top);
out.writeInt(right); out.writeInt(bottom);
}
private void readFromParcel(Parcel in) {
left = in.readInt(); top = in.readInt();
right = in.readInt(); bottom = in.readInt();
}
}
客戶端透過 bindService() 建立連線,並在 ServiceConnection 的 onServiceConnected() 中把原始的 IBinder 轉成介面物件:
mService = IRemoteService.Stub.asInterface(service);
之後即可如呼叫本地方法一般使用。若要解除連線,別忘了先反註冊 callback,再呼叫 unbindService()。另外要注意:IPC 呼叫是透過每個行程各自的執行緒池分派的,因此 callback 中的程式碼不會跑在主執行緒上——要更新 UI,必須用 Handler 跳回 UI 執行緒。
掌握這幾個要點,你就能把服務設計成真正可被其他應用重用的元件,而不只是一個關起門來自己跑的背景程式。
Android開發免費資源與工具整理:書籍、模擬器、UI設計與範例
免費資源是學習Android的加速器 Android開發門檻相對友好,原因之一是社群提供大量免費書籍、範例、模擬器與UI素材。對初學者而言,善用這些資源,可以快速建立開發
Android 架構解析:從 Linux 核心到應用層的五層結構
Android 作業系統是一個堆疊式的軟體架構。整體可以分成五個部分、四個主要層次,由下而上分別是 Linux 核心、函式庫與執行環境、應用程式架構,最上層則是應用程式本
Android UI 設計入門:View、ViewGroup 與 Layout 的關係
Android 由作業系統、中間件、使用者介面與應用軟體組成,而一個應用能否受使用者青睞,UI 往往是最後的決勝點。本文梳理 Android UI 體系的三個核心概念,並
Activity 的四種啟動模式(1):先弄懂生命週期
建議先讀過下面兩篇,才能比較好地理解啟動模式: Android 的行程與執行緒模型(cnblogs.com/ghj1976)——其中對「Android 單執行緒模型」的描