編輯:初階開發
想要設計出更優秀的 Android 軟體,就應該熟悉並掌握 AIDL 的 IPC(行程間通訊)機制。它能讓你撰寫的元件像 Windows 的 ActiveX COM 一樣便於複用,並提供類似 Symbian 那樣的服務端機制。而「服務」正好能很好地解決在後台運行、無 UI 視窗的需求。本篇以一個修改自 Android SDK 文件的範例,帶讀者走一遍 AIDL 的完整流程。
首先建立一個副檔名為 .aidl 的檔案(例如 android123.aidl),宣告一個介面。以下以銀行 ATM 程式為例:
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);
}
在服務端明確提供一個對外導出的介面,供用戶端綁定:
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;
}
// 第一個介面
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」即可。以下是一個實作 Parcelable 的範例:
import android.os.Parcel;
import android.os.Parcelable;
public final class Rect implements Parcelable {
public int left;
public int top;
public int right;
public int 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 Rect() { }
private Rect(Parcel in) {
readFromParcel(in);
}
// 將資料寫入 Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeInt(left);
out.writeInt(top);
out.writeInt(right);
out.writeInt(bottom);
}
// 從 Parcel 讀取資料
public void readFromParcel(Parcel in) {
left = in.readInt();
top = in.readInt();
right = in.readInt();
bottom = in.readInt();
}
}
用戶端透過 ServiceConnection 來建立與服務的連線,並用 IBinder 取得介面的用戶端代理:
public class RemoteServiceBinding extends Activity {
IRemoteService mService = null;
ISecondary mSecondaryService = null;
private boolean mIsBound;
// 建立連線
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// 透過 Stub.asInterface 取得用戶端的介面代理
mService = IRemoteService.Stub.asInterface(service);
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) { }
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
// 綁定服務
bindService(new Intent(IRemoteService.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
// 解除綁定
unbindService(mConnection);
// 回呼:注意 IPC 回呼執行於執行緒池,更新 UI 需透過 Handler
private IRemoteServiceCallback mCallback =
new IRemoteServiceCallback.Stub() {
public void valueChanged(int value) {
mHandler.sendMessage(
mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};
}
AIDL 的核心價值在於「介面導出、跨行程通訊」。切記一個關鍵細節:透過 IPC 送達的回呼是在各行程的執行緒池中派發的,因此回呼方法內若要更新 UI,必須借助 Handler 切換回主執行緒,否則會引發例外。掌握 AIDL 與 Parcelable,你就能打造出具備服務端能力、可跨元件複用的高階 Android 應用。
Android 編程實作:捏泡泡小遊戲
這是一個簡單的 Android 小遊戲,主要用來示範 Activity、自訂元件、觸控事件與音效播放。專案包含 Bubble、Vacuole 與 Sound 三個主要類別
Android 調試工具 Monkey 壓力測試實戰
許多 Android 開發者因為沒有充分測試自己的軟體,導致應用頻頻出現 FC(Force Close)。其實,我們可以借助 Android 固件自帶的 Monkey 工
Android 開發工具巡禮:App Inventor
隨著 Android 設備的日益普及,為讓更多人參與到 Android 軟體開發中來,Google 官方推出了一款名為 App Inventor 的開發工具,讓用戶無需編
Android 連線時顯示下載進度條
在 Android 應用中,若需要從網路取得資料,常會使用進度條提示使用者目前仍在處理中。這能避免畫面看起來像卡住,也能提升操作回饋。基本思路可以先顯示 Progress