退出APP、清空任务栈、杀掉主进程

    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 {
            finishAllActivity();
            if (finishAndRemoveAllTasks()) {
                activityStack.clear();
            }
            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);
        }
    }

猜你喜欢

转载自blog.csdn.net/waplyj/article/details/83990639
今日推荐