我發現用這種方法進行密切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"/>
如果你也需要關閉狀態欄你可以試試這個代碼:
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 2個權限。 xml
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
可能重複http://stackoverflow.com/a/17774643/2520370 – Steve
i sa我使用這種方法的身份證,但它不是我正在尋找的,因爲我們可以使用此解決方案訪問最近的任務 – user2641131
如果您想禁用最近的對話框,您將需要利用漏洞,因爲它是故意不可能的。 – zapl