为什么getRunningTasks不将RecentApplicationsActivity识别为正在运行的任务?

时间:2014-05-09 10:59:57

标签: android activity-manager

ActivityManager am = (ActivityManager)this.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(5);

这是我的主屏幕应用程序中用于获取正在运行的活动的代码。

长按HOME键时,会显示最近使用的应用列表(名为RecentApplicationsActivity的活动)。但它没有列在上面代码中的“任务”中。

为什么这不起作用?

1 个答案:

答案 0 :(得分:2)

因为它不是一个Activity而是一个显示RecentApplications的Dialog。它叫做RecentApplicationsDialog。 您可以查看AOSP中的代码。

https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java

此对话框由PhoneWindowManager处理,由WindowManagerService管理。

https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

private void handleLongPressOnHome() {

        if (mLongPressOnHomeBehavior < 0) {
            mLongPressOnHomeBehavior
                    = mContext.getResources().getInteger(R.integer.config_longPressOnHomeBehavior);
            if (mLongPressOnHomeBehavior < LONG_PRESS_HOME_NOTHING ||
                    mLongPressOnHomeBehavior > LONG_PRESS_HOME_RECENT_SYSTEM_UI) {
                mLongPressOnHomeBehavior = LONG_PRESS_HOME_NOTHING;
            }
        }

        if (mLongPressOnHomeBehavior != LONG_PRESS_HOME_NOTHING) {
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);

            // Eat the longpress so it won't dismiss the recent apps dialog when
            // the user lets go of the home key
            mHomeLongPressed = true;
        }

        if (mLongPressOnHomeBehavior == LONG_PRESS_HOME_RECENT_DIALOG) {
            showOrHideRecentAppsDialog(RECENT_APPS_BEHAVIOR_SHOW_OR_DISMISS);
        } else if (mLongPressOnHomeBehavior == LONG_PRESS_HOME_RECENT_SYSTEM_UI) {
            try {
                IStatusBarService statusbar = getStatusBarService();
                if (statusbar != null) {
                    statusbar.toggleRecentApps();
                }
            } catch (RemoteException e) {
                Slog.e(TAG, "RemoteException when showing recent apps", e);
                // re-acquire status bar service next time it is needed.
                mStatusBarService = null;
            }
        }
    }

用于显示此Dialog的Context不属于任何正常任务,而是使用系统上下文来显示此Dialog。