5

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

BoredAndroidDeveloper
  • 1,251
  • 1
  • 11
  • 26
  • Your title says simulate, but your post says you want to actually launch the recent apps screen... which is it? – bwoogie Sep 10 '12 at 02:32
  • I just want to launch the menu. That could involve simulating the keypress OR using an intent. Not sure what is possible – BoredAndroidDeveloper Sep 10 '12 at 02:44
  • Give base/core/java/android/app/ActivityManager.java a look, I think that might have it in there. Just a guess though. – Connor Tumbleson Sep 10 '12 at 02:55
  • 1
    this question asks the same thing with better answers: https://stackoverflow.com/questions/14267482/android-programmatically-open-recent-apps-dialog – ThunderWiring Sep 28 '17 at 14:45

3 Answers3

5

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.

codezjx
  • 9,012
  • 5
  • 47
  • 57
  • I gave you an upvote - but I was looking for a way to do it on any device and this is dependent on stock android. I don't think there is a global way to do this without root or special permissions on all devices. – BoredAndroidDeveloper Sep 08 '15 at 20:33
  • 1
    Yeah, that's true. But if you want to find a global way, I think you can try to send a keypad KeyEvent.KEYCODE_APP_SWITCH to invoke the RecentsActivity. – codezjx Sep 09 '15 at 01:13
3

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);
1

No, this is not possible (mostly due to the many ways this is handled between the various esoteric Android OSs). However, you could get a list of running processes and make your own recent apps list. (Perhaps this answer or this one will help you.)

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • I was worried that would be the case. I know you can inject the home button press. I was hoping that if you couldn't launch it via an intent then you could simulate a long press of the home button. – BoredAndroidDeveloper Sep 10 '12 at 03:11
  • Would it be possible if I limit the version of android to >3 or even >4? – BoredAndroidDeveloper Sep 10 '12 at 03:15
  • I'm afraid not. There is a recent apps feature in Android 2.x (hold home button), and a limited feature in 3.x (no eliminating tasks), then the fully-featured version seen in 4.x. Unfortunately, these are controlled exclusively by the OS. It ***may*** be possible with rooting; I can't speak to that experience. – Cat Sep 10 '12 at 03:23
  • I'll give credit here since I also don't see a way to do this. I'll post back if I ever find out anything to the contrary – BoredAndroidDeveloper Sep 11 '12 at 18:34
  • @BoredAndroidDeveloper Sounds good! I'm always happy to be proven wrong in favor of new knowledge. – Cat Sep 11 '12 at 20:40