I would like to launch the menu that shows the recently used apps.
I've tried looking at logcat while hitting the button hoping there was some intent I could launch but no luck.
I know that on some phones it is a dedicated button and it can also happen by long-pressing the home button. Is there any way that I can launch it programmatically?
EDIT: Updated the title to be more accurate
When you press "Recent Apps" button, the logcat will output following message:
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
So, we can simulate of this operation by programmatically. (Don't forget to set the flags to 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);
For more infomation about RecentsActivity
, please read the source code here.
just like what @Floerm post, this piece of code only work below android N , android N has change IStatusBarService api, we can't access like this.
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();
}}
If you want to launch the recent apps in Android N, you need to use AccessibilityService.
accessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With