如何在Android中启动最近的应用程序菜单?

时间:2012-09-10 02:30:05

标签: android android-intent

我想启动显示最近使用过的应用的菜单。

我试着在按下按钮时看着logcat,希望有一些意图我可以启动但没有运气。

我知道在某些手机上它是一个专用按钮,也可以通过长按主页按钮来实现。有什么方法可以通过编程方式启动它吗?

编辑:更新标题更准确

3 个答案:

答案 0 :(得分:4)

当您按“最近的应用”按钮时,logcat将输出以下消息:

568-716/system_process I/ActivityManager﹕ START u0 {act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity} from pid 627
    --------- beginning of /dev/log/main
568-582/system_process I/ActivityManager﹕ Displayed com.android.systemui/.recent.RecentsActivity: +215ms

因此,我们可以通过编程方式模拟此操作。 (别忘了将标志设置为0x10800000):

Intent intent = new Intent ("com.android.systemui.recent.action.TOGGLE_RECENTS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity"));
startActivity (intent);

有关RecentsActivity的更多信息,请阅读源代码here

答案 1 :(得分:2)

就像@Floerm发布的那样,这段代码只能在android N下面工作,android N已经改变了IStatusBarService api,我们无法像这样访问。

private void openRecentApps() {
try {
    Class serviceManagerClass = Class.forName("android.os.ServiceManager");
    Method getService = serviceManagerClass.getMethod("getService", String.class);
    IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
    Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
    Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[]{retbinder});
    Method clearAll = statusBarClass.getMethod("toggleRecentApps");
    clearAll.setAccessible(true);
    clearAll.invoke(statusBarObject);
} catch (Exception e) {
    e.printStackTrace();
}}

如果您想在Android N中启动最近的应用,则需要使用AccessibilityService。

accessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);

答案 2 :(得分:0)

不,这是不可能的(主要是由于各种深奥的Android操作系统之间的处理方式很多)。但是,您可以获取正在运行的进程列表并创建自己的最新应用程序列表。 (也许this answerthis one会对您有所帮助。)