編輯:關於 Android 程式設計
以下分析基於 MTK 的 Android 4.4 原生 SystemUI。MTK 版本與 Google 原生版本有細微差異,但整體架構是一致的。
這是 SystemUI 系列的第一篇,先從最簡單的 PowerUI 著手。源碼路徑:/frameworks/base/packages/SystemUI,程式目錄結構如下:

匯入 Eclipse 後會出現大量錯誤,原因多半是找不到 framework 的內部類別與資源(例如 com.android.internal.R)——這是正常的,因為這些符號只有在平台完整原始碼樹中才存在。修改完之後用 mmm 編譯成模組,再 push 到 eng 版本的手機上除錯,push 後需重啟才會生效。
在 AndroidManifest.xml 中找不到任何 Activity 註冊:
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
所以整個程式是被外部啟動的,具體來說是透過 SystemUIService。而拉起它的地方在:
/frameworks/base/services/java/com/android/server/SystemServer.java
static final void startSystemUi(Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.android.systemui",
"com.android.systemui.SystemUIService"));
context.startServiceAsUser(intent, UserHandle.OWNER);
}
也就是說,SystemUI 在 SystemServer 啟動過程中就被拉起來了,它的生命週期與系統本身綁在一起。
package com.android.systemui;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.util.HashMap;
public class SystemUIService extends Service {
private static final String TAG = "SystemUIService";
/** 要啟動的元件清單 */
private final Class<?>[] SERVICES = new Class[] {
com.android.systemui.recent.Recents.class,
com.android.systemui.statusbar.SystemBars.class,
com.android.systemui.usb.StorageNotification.class,
com.android.systemui.power.PowerUI.class,
com.android.systemui.media.RingtonePlayer.class,
com.android.systemui.settings.SettingsUI.class,
};
/** 持有我們啟動的這些元件的參考 */
private final SystemUI[] mServices = new SystemUI[SERVICES.length];
@Override
public void onCreate() {
HashMap<Class<?>, Object> components = new HashMap<Class<?>, Object>();
final int N = SERVICES.length;
for (int i = 0; i < N; i++) {
Class<?> cl = SERVICES[i];
Log.d(TAG, "loading: " + cl);
try {
mServices[i] = (SystemUI) cl.newInstance();
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
}
mServices[i].mContext = this;
mServices[i].mComponents = components;
Log.d(TAG, "running: " + mServices[i]);
mServices[i].start();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
這個設計很值得注意:清單裡的 Recents、SystemBars、StorageNotification、PowerUI、RingtonePlayer、SettingsUI 全都繼承自 SystemUI,在 onCreate() 中被逐一建立實例並呼叫 start()。
另外 mComponents 這個共用 HashMap 是元件間溝通的通道——例如 StorageNotification 需要存取通知管理器時,可以從這個表裡取得共享實例,避免各自 new 一份。
接著看最簡單的 com.android.systemui.power.PowerUI。它的 start() 方法主要在做一件事:註冊監聽器。
public void start() {
mLowBatteryAlertCloseLevel = mContext.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
mLowBatteryReminderLevels[0] = mContext.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
mLowBatteryReminderLevels[1] = mContext.getResources().getInteger(
com.android.internal.R.integer.config_criticalBatteryWarningLevel);
final PowerManager pm = (PowerManager)
mContext.getSystemService(Context.POWER_SERVICE);
mScreenOffTime = pm.isScreenOn() ? -1 : SystemClock.elapsedRealtime();
// 註冊廣播意圖
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
// M: 設定變更時顯示電量
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
// M: PowerOffAlarm 響鈴時隱藏低電量對話框
filter.addAction("android.intent.action.normal.boot");
filter.addAction("android.intent.action.ACTION_SHUTDOWN_IPO");
mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
}
邏輯非常直白:透過 ACTION_BATTERY_CHANGED 判斷電量高低,低於門檻時給使用者提示;其他的 ACTION 則各自對應特定的處理。門檻值不是寫死的,而是從 com.android.internal.R.integer 讀取,這也是為什麼 OEM 可以只改資源檔就調整行為,而不必動 Java 程式碼。
實務結論:如果跟電源相關的功能需要客製化或新增監聽項目,這個類別就是修改的入口。想知道 SystemUI 怎麼啟動、有哪些子模組、電量提示怎麼觸發,從這裡往下讀最有效率。
Android 網絡安全:Netfilter 與 iptables 的關係與工作流程
在 Android 與 Linux 系統中,Netfilter 是核心網路封包處理框架,而 iptables 則是使用者層用來設定與管理規則的工具。兩者相輔相成,共同完成
Android 記憶體洩露排查與優化實錄:MAT 與 LeakCanary
背景 專案在使用一段時間後,或反覆開啟與關閉某些頁面時,記憶體會持續上升,並頻繁觸發 GC。即使靜置一段時間,問題仍明顯存在。 圖:操作時的記憶體使用情況。 圖:靜置
Android ueventd 解析:裝置節點建立與冷熱插拔機制
前言:裝置節點為何重要 在 Android 中,應用程式通常透過裝置節點存取硬體驅動。與傳統 Linux 不同,Android 的根檔案系統在啟動初期並沒有完整的 /de
Android 入門:SurfaceView 基本用法與繪圖效率分析
前面介紹 MediaPlayer 時稍微提過 SurfaceView。由於它能直接從記憶體或 DMA 等硬體介面取得圖像資料,SurfaceView 在繪圖這件事上佔有相