Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Programmatically open "Recent Apps" dialog

Tags:

android

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!

like image 619
Kenny Avatar asked Jan 10 '13 21:01

Kenny


2 Answers

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

like image 103
Ion Aalbers Avatar answered Sep 21 '22 03:09

Ion Aalbers


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.

like image 42
Raghav Sood Avatar answered Sep 18 '22 03:09

Raghav Sood