13

I would like to be able to open the "Recent Apps" dialog from my application. This is the dialog that is opened by long-pressing the home button. I am programming for Android 4.1 or earlier. I found a way to do it by implementing a custom AccessibilityService and calling AccessibilityService.performGlobalAction(GLOBAL_ACTION_RECENTS), but this requires enabling accessibility on the phone, which is not very desirable. Is there any other way to open this dialog from an app?

Thanks for the help!

Kenny
  • 356
  • 1
  • 3
  • 7

3 Answers3

37

This code won't work on Nougat or later

It is possible to trigger the recent apps activity.

The StatusBarManagerService implements an public method which you can use through reflection.

You can use the following code:

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

Have fun

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
  • 1
    Using reflection is generally a bad idea as there is no guarantee that it works, or will continue to work on all versions of Android. – Raghav Sood Apr 12 '13 at 10:18
  • 4
    If using the above code, be sure to wrap it in a `try / catch` – Chris Lacy Apr 19 '13 at 05:52
  • The above code is not working for Android2.2 & 2.3, is there any other alternative way to make it work. – Sathish Jul 24 '13 at 06:10
  • @Sathish Yeah, in Android < 3.0, the recent apps are shown in an dialog. (http://androidxref.com/2.3.6/xref/frameworks/base/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java) AFAIK you cannot launch this. – Ion Aalbers Oct 21 '13 at 07:12
  • @IonAalbers Is there a way to retrieve the screenshots of the recent apps the OS took when it was going into pause mode? – Naruto Dec 23 '13 at 17:23
  • This really works! Thanks :) Is there a way to clear the recent app list (without opening it)? – Umair Dec 24 '13 at 13:02
  • @IonAalbers , if you can explain this code snippet in detail, it would be appreciated. – Krishna Gupta Sep 11 '14 at 22:28
  • Just wrap the code in a try/catch and it should work. Kuddos to your skills!! – AouledIssa Oct 28 '16 at 16:33
  • 1
    This code is not work in nought, it's show error java.lang.NoSuchMethodException: toggleRecentApps [], Please help if anyone solve this problem – akhil batlawala May 22 '18 at 05:44
6

You cannot access that. However, it isn't super hard to roll your own. The getRecentTasks() method returns a list of recently run apps. Simply take the list and add your own UI to it.

One advantage to this is that the default one, at least on older versions of Android, only shows you about 8 apps. If you roll your own can show as many as you want.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 3
    remember that some vendors modifies recent apps dialog, so your dialog wont look similar for those users. – Leonidos Jan 14 '13 at 12:08
  • @Leonidos True, but its the best solution I've been able to come up with without using the `AccessibilityService`. – Raghav Sood Jan 14 '13 at 12:10
  • 1
    Also note that these API's are meant for debugging only. I think they cause performance issues if used wrongly. Google Inc does not want devs to [use it in production code](https://www.google.co.in/search?hl=en&sugexp=les%3B&gs_rn=1&gs_ri=serp&pq=cats&cp=9&gs_id=7i&xhr=t&q=ocelot+kittens&client=firefox-aurora&hs=sDe&rls=org.mozilla:en-US:unofficial&bav=on.2,or.r_gc.r_pw.r_qf.&bvm=bv.41248874,d.aGc&biw=1192&bih=645&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi&ei=X4T-UMSxDq2gigeMg4GwDw). – Reno Jan 22 '13 at 12:20
  • 1
    It is possible, check out my answer – Ion Aalbers Apr 12 '13 at 06:49
  • Unfortunately, the method is deprecated as of API 21. See https://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks(int,%20int) – JM Lord May 08 '17 at 14:15
  • Not alone deprecated but also returns only the current running app (size: 1) – Dr.jacky Mar 02 '23 at 11:15
5

This can be done using the TOGGLE_RECENTS Intent.

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

Note Package would be changed basis on Api level. See here.

Android 4.1: "com.android.internal.policy.impl.RecentApplicationsDialog"
Android 4.2 - 4.4: "com.android.systemui.recent.RecentsActivity"
Android 5.0 - 7.1: "com.android.systemui.recents.RecentsActivity" ("s" letter was added)
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
Ava
  • 2,038
  • 3
  • 23
  • 45
  • Thanks; this worked on Android 5.1.1 for me. However, the resulting activity looked different to the normal Android 5 one. Also, I think you missed a closing parenthesis on your second line. – Sam Jun 22 '15 at 11:59
  • for some the package changes to com.android.systemui.recents.RecentsActivity – Zar E Ahmer Dec 03 '18 at 06:49
  • Got this issue Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS cmp=com.android.systemui/.recents.RecentsActivity – Makvin Dec 07 '18 at 12:40