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

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

长按该HOME键时,会出现最近使用的应用程序列表(带有名为 的活动RecentApplicationsActivity)。但它没有在上述代码的“任务”中列出。

为什么这不起作用?

4

1 回答 1

2

因为它不是一个活动,而是一个显示最近应用程序的对话框。它称为RecentApplicationsDialog。您可以在 AOSP 中查看代码。

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

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

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。

于 2014-06-24T23:37:03.783 回答