按下任務物理鍵,會顯示最近打開的App列表,能夠用來切換多任務,如何經過代碼來實現這種效果呢?
相似下面這樣:
android
利用反射便可實現這個功能,示例代碼以下:web
private void showRecentApps() {
Class serviceManagerClass;
try {
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 (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}