編輯:關於 Android 程式設計
學任何東西之前,先想清楚它為什麼存在。關於 AIDL,很多人腦中的印象是「可以跨進程呼叫其他應用」。但能做到這件事的技術並不少:
那 AIDL 存在的意義是什麼?官方文件講得很乾脆:
Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger.
翻譯成白話:只有當你允許來自不同應用程式的用戶端存取你的服務,而且需要處理多執行緒問題時,才必須使用 AIDL。不需要跨應用並發,就用 Binder;需要跨進程但不必處理多執行緒,用 Messenger 就好。
關鍵差異因此非常清楚:AIDL 是為多執行緒、多用戶端並發存取而設計的;Messenger 是單執行緒序列化處理。一句話就說明白了。
// IRemoteService.aidl
package com.example.android;
interface IRemoteService {
/** 取得服務所在的行程 ID */
int getPid();
/** 示範 AIDL 可用的基本型別 */
void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString);
}
存檔之後,Android 編譯器會自動在 gen 目錄下產生 IRemoteService.java。
建立 DDService.java,並在 AndroidManifest.xml 註冊,加上對外的 Action:
package com.example.service;
import com.example.android.IRemoteService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
public class DDService extends Service {
@Override
public void onCreate() {
super.onCreate();
System.out.println("DDService onCreate........ Thread: "
+ Thread.currentThread().getName());
}
@Override
public IBinder onBind(Intent arg0) {
System.out.println("DDService onBind");
return mBinder;
}
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid() {
System.out.println("Thread: " + Thread.currentThread().getName());
System.out.println("DDService getPid");
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
System.out.println("Thread: " + Thread.currentThread().getName());
System.out.println("basicTypes aDouble: " + aDouble
+ " anInt: " + anInt
+ " aBoolean " + aBoolean
+ " aString " + aString);
}
};
}
把服務端跑起來,重點觀察 log 裡的執行緒名稱。
另開一個工程,記得把同一份 AIDL 檔複製進來(它是標準介面定義,兩邊必須一致):
package com.example.aidlclient;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.view.View;
import com.example.android.IRemoteService;
public class MainActivity extends Activity {
private IRemoteService remoteService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { }
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
remoteService = IRemoteService.Stub.asInterface(service);
try {
int pid = remoteService.getPid();
int currentPid = Process.myPid();
System.out.println("currentPID: " + currentPid + " remotePID: " + pid);
remoteService.basicTypes(12, 1223L, true, 12.2f, 12.3, "我們的愛,我明白");
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("bind success! " + remoteService.toString());
}
};
public void buttonClick(View view) {
System.out.println("begin bindService");
Intent intent = new Intent("duanqing.test.aidl");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
點擊用戶端按鈕,觀察兩邊的 log:
onCreate 跑在 Thread: main——這是服務自己的主執行緒;getPid() 時,服務端在 Thread: Binder2 中執行;basicTypes() 時,服務端在 Thread: Binder1 中執行。兩個請求落在不同執行緒上,這正是 AIDL 的價值所在:它為每個進來的用戶端請求建立執行緒池,讓並發成為可能。若改用 Messenger,這些呼叫會被排成一列依序處理,第二個請求必須等第一個做完。
所以選擇很簡單:需要並發與多執行緒安全,用 AIDL;只是被動接收指令、順序不重要,用 Messenger 更省事。
Android 建立選單選項 Menu 與取得螢幕解析度
在 Android 應用中,選單(Menu)是使用者與程式互動的重要入口;而取得裝置的螢幕解析度,則是進行自適應排版時常需要的資訊。本文示範這兩個實用技巧的實作方式。
SharedPreferences 儲存設定
SharedPreferences 提供簡單的鍵值儲存方式,很適合保存設定、開關狀態或小型資料。寫入先取得 SharedPreferences,再透過 Editor 呼叫
Android 廣播機制完全指南:從標準廣播、有序廣播到本地廣播
廣播接收器(Broadcast Receiver)是 Android 四大元件之一,也是應用彼此溝通、回應系統事件的重要管道。本文將系統性地整理廣播的類型、註冊方式,以及
Android 事件處理機制入門:基於監聽的模型與四種監聽器寫法
Android 的事件處理機制主要分兩大類: 基於監聽:為界面元件綁定特定的事件監聽器。 基於回呼:直接覆寫元件或 Activity 的特定回呼方法。Android 絕