Android: Programmatically open “Recent Apps” dialog

前端 未结 3 1580
梦毁少年i
梦毁少年i 2020-12-05 11:42

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 Andr

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 12:23

    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

提交回复
热议问题