退出APP、清空任務棧、殺掉主程序
阿新 • • 發佈:2018-12-21
private static boolean finishAndRemoveAllTasks() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return false; } ActivityManager am = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE); if (am == null) { return false; } try { List<ActivityManager.AppTask> appTasks = am.getAppTasks(); for (ActivityManager.AppTask appTask : appTasks) { //CqrLog.debug("will finish and remove task: id=" + appTask.getTaskInfo().id); appTask.finishAndRemoveTask(); } return true; } catch (SecurityException e) { //CqrLog.debug(e); } return false; } /** * 應用程式退出 * * @param forceKill 是否強制殺掉程序 */ public static void exitApp(boolean forceKill) { //CqrLog.debug("will exit app: forceKill=" + forceKill); try { if (finishAndRemoveAllTasks()) { activityStack.clear(); } else { finishAllActivity(); } if (!forceKill) { return; } //退出JVM(Java虛擬機器),釋放所佔記憶體資源,0表示正常退出(非0的都為異常退出) System.exit(0); //從作業系統中結束掉當前程式的程序 int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid); } catch (Throwable ignore) { System.exit(-1); } }