我想启动显示最近使用的应用程序的菜单。
我尝试在按下按钮时查看logcat,希望有一些意图可以启动,但没有运气。
我知道在一些手机上,它是一个专用按钮,并且也可以通过长按主页按钮来发生。有没有办法以编程方式启动它?
编辑:更新标题以更准确
我尝试在按下按钮时查看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
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
的更多信息,请阅读此处的源代码。
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);