Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch the recent apps menu in android?

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

like image 876
BoredAndroidDeveloper Avatar asked Sep 10 '12 02:09

BoredAndroidDeveloper


2 Answers

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.

like image 167
codezjx Avatar answered Sep 19 '22 01:09

codezjx


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);
like image 42
Corer Zhang Avatar answered Sep 19 '22 01:09

Corer Zhang