Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.4 近期任務列表:RecentsActivity 與 RecentTasksLoader 解析

編輯:關於 Android 程式設計

本文針對 Android 4.4 的「近期應用」機制做源碼級說明,涉及兩個關鍵檔案。

RecentsActivity:點擊空白處回到桌面

檔案位置: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。

RecentTasksLoader:近期任務的載入與過濾

檔案位置: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 作為結束哨兵。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved