編輯:關於 Android 程式設計
本文針對 Android 4.4 的「近期應用」機制做源碼級說明,涉及兩個關鍵檔案。
檔案位置:frameworks/base/packages/SystemUI/src/com/android/systemui/recent/RecentsActivity.java
public void dismissAndGoHome() {
if (mRecentsPanel != null) {
Intent homeIntent = new Intent(Intent.ACTION_MAIN, null);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// 以當前使用者身分跳轉到系統最高層的 Launcher
startActivityAsUser(homeIntent, new UserHandle(UserHandle.USER_CURRENT));
}
}
這段的作用很直觀:在近期任務介面點擊空白處時,發出一個帶 CATEGORY_HOME 的 Intent,把使用者帶回桌面。關鍵是 startActivityAsUser 搭配 USER_CURRENT,確保在多使用者環境下會回到目前使用者自己的 Launcher。
檔案位置:frameworks/base/packages/SystemUI/src/com/android/systemui/recent/RecentTasksLoader.java
近期任務清單就是在這裡組出來的。核心邏輯在 doInBackground 中:
protected Void doInBackground(Void... params) {
// 分兩階段載入:先更新一屏的項目讓 UI 盡快有內容,
// 再補上其餘項目
final int origPri = Process.getThreadPriority(Process.myTid());
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
final PackageManager pm = mContext.getPackageManager();
final ActivityManager am = (ActivityManager)
mContext.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RecentTaskInfo> recentTasks =
am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
int numTasks = recentTasks.size();
// 找出目前桌面的 ActivityInfo,用來比對是否為 Launcher
ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(pm, 0);
boolean firstScreenful = true;
ArrayList<TaskDescription> tasks = new ArrayList<TaskDescription>();
for (int i = 0, index = 0; i < numTasks && index < MAX_TASKS; ++i) {
if (isCancelled()) {
break;
}
final ActivityManager.RecentTaskInfo recentInfo = recentTasks.get(i);
Intent intent = new Intent(recentInfo.baseIntent);
if (recentInfo.origActivity != null) {
intent.setComponent(recentInfo.origActivity);
}
// 關鍵:不要載入目前的桌面 Activity
if (isCurrentHomeActivity(intent.getComponent(), homeInfo)) {
continue;
}
// 也不要載入 SystemUI 自己
if (intent.getComponent().getPackageName().equals(mContext.getPackageName())) {
continue;
}
TaskDescription item = createTaskDescription(recentInfo.id,
recentInfo.persistentId, recentInfo.baseIntent,
recentInfo.origActivity, recentInfo.description);
if (item != null) {
while (true) {
try {
tasksWaitingForThumbnails.put(item);
break;
} catch (InterruptedException e) {
}
}
tasks.add(item);
// 滿一屏就先回報一次,讓縮圖能優先載入
if (firstScreenful && tasks.size() == mNumTasksInFirstScreenful) {
publishProgress(tasks);
tasks = new ArrayList<TaskDescription>();
firstScreenful = false;
}
++index;
}
}
if (!isCancelled()) {
publishProgress(tasks);
if (firstScreenful) {
// 保證一定會有兩次更新
publishProgress(new ArrayList<TaskDescription>());
}
}
while (true) {
try {
tasksWaitingForThumbnails.put(new TaskDescription());
break;
} catch (InterruptedException e) {
}
}
Process.setThreadPriority(origPri);
return null;
}
1. 過濾掉 Launcher 與自己。透過 isCurrentHomeActivity() 比對 homeInfo,把桌面排除在近期任務之外;同時也把 SystemUI 自身的 package 排除。否則使用者會在近期任務清單裡看到一個「桌面」項目,體驗很怪。如果你要客製化這個行為,這兩個 continue 就是關鍵位置。
2. 兩階段載入。firstScreenful 這個旗標是為了效能:先只組出一屏的項目就呼叫 publishProgress() 更新 UI,讓縮圖與圖示能優先載入,其餘項目稍後補上。註解裡原本的 break 被拿掉,就是為了讓迴圈繼續跑完剩下的部分。
3. 執行緒優先級。進入背景工作前把執行緒優先級降為 THREAD_PRIORITY_BACKGROUND,結束後再還原。這是 Android 背景任務的標準做法,可以避免搶走 UI 執行緒的資源。
4. 阻塞佇列同步縮圖任務。用 tasksWaitingForThumbnails 搭配 put() 的 InterruptedException 重試迴圈,確保縮圖載入執行緒不會漏掉任何一筆任務;最後再放一個空的 TaskDescription 作為結束哨兵。
Android 安全審計工具 MobiSecLab 上手實測
分析 Android 應用時,本地端有許多工具可以勝任。本文介紹的是另一條路——線上化的惡意軟體檢測與靜態分析平台 MobiSecLab。國內相關介紹不多,但實際使用量並
Android Wear 設計指南:Suggest 與 Demand 兩大核心
來源:https://developer.android.com/design/wear/index.html 為什麼手錶不能照搬手機的設計 為可穿戴設備設計應用,和為手
Android 5.0 學習筆記:AnimatedVectorDrawable 入門
幾行程式碼加幾個設定檔,就能做出流程順暢、無縫銜接的向量動畫。 範例程式碼:animated-vector-drawable VectorDrawable 從 And
Android 音樂播放器製作(三):加入控制台
運行效果 本篇接續上一篇 Android 音樂播放器製作。其中這個效果(圓形 ImageView 與控件匀速旋轉)在之前的博客已經寫過,這裡就不細說了: 具體實現 首