我发现使用此方法关闭RecentsActivity 的最佳方法
private static final String RECENTS_ACTIVITY = "com.android.systemui.recent.RecentsActivity";
private static final String SYSTEM_UI_PACKAGE_NAME = "com.android.systemui";
private void closeRecents() {
sendBroadcast(new Intent("com.android.systemui.recent.action.CLOSE"));
Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
ComponentName recents = new ComponentName(SYSTEM_UI_PACKAGE_NAME, RECENTS_ACTIVITY);
closeRecents.setComponent(recents);
startActivity(closeRecents);
}
那么,何时以及如何使用这种方法呢?
在您的活动中覆盖 onPause() 并首先在 ActivityManager 中检查最近的活动。这种方法并不准确,但我没有找到更好的方法:
private ComponentName getTopActivity(){
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo runningTaskInfo = taskInfo.get(0);
ComponentName topActivity = runningTaskInfo.topActivity;
return topActivity;
}
@Override
protected void onPause() {
super.onPause();
if (getTopActivity().getClassName().equals(RECENTS_ACTIVITY)){
closeRecents();
}else {
validateAgainWithDelay(300);
}
}
private void validateAgainWithDelay(long delay){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (getTopActivity().getClassName().equals(RECENTS_ACTIVITY)) {
closeRecents();
}
}
},delay);
}
我在 android 4.4.2 上进行了检查,它可以工作,希望这对你也有帮助。
您还需要在 AndroidManifest.xml 中添加权限
<uses-permission android:name="android.permission.GET_TASKS"/>
如果您还需要关闭 StatusBar,您可以尝试以下代码:
private final Handler uiHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what==REMOVE_STATUS_BAR){
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
collapseStatusBar();
return true;
}
return false;
}
});
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus){
uiHandler.sendEmptyMessage(REMOVE_STATUS_BAR);
uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 500);
uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 1000);
uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 2000);
}
}
private void collapseStatusBar() {
try {
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method collapse = statusbarManager.getMethod(hasCollapseMethod(statusbarManager) ? "collapse" : "collapsePanels");
collapse.setAccessible(true);
collapse.invoke(service);
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean hasCollapseMethod(Class<?> statusbarManager) {
for (Method method : statusbarManager.getMethods()) {
if (method.getName().equals("collapse")) {
return true;
}
}
return false;
}
此方法还需要 AndroidManifest.xml 中的 2 个权限
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>