Show recent apps programmatically on android oreo

1.2k views Asked by At

I want to somehow immitate the recent apps hard button click which opens up the native android recent apps screen.

I do following currently:

boolean success = showRecents1(c);
if (!success) {
    showRecents2(c);
}

This works on a lot of devices, but on the android oreo emulator it does not work. Does anyone know a solution that works on android oreo as well?

private boolean showRecents1(Context c) {
    try {
        Intent intent = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
        intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity"));
        c.startActivity(intent);
        return true;
    } catch (Exception e) {
        L.e(e);
    }
    return false;
}

private boolean showRecents2(Context c) {
    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);
        return true;
    } catch (Exception e) {
        L.e(e);
    }
    return false;
}
0

There are 0 answers