美文网首页
判断应用是否运行以及Activity任务栈状态

判断应用是否运行以及Activity任务栈状态

作者: 钦_79f7 | 来源:发表于2019-12-20 16:36 被阅读0次

    基本介绍

    通过ActivityManager获取到当前运行中的进程列表(getRunningAppProcesses),每个进程的名字与对应应用ApplicationId相同。

    如果当前运行进程列表中不存在与应用applicationId相同的进程,那么应用的进程肯定是处于被杀死的状态。

    反之的话(即进程运行中),存在两种情况:

    1. Activity任务栈不为空

    2. Activity任务栈为空

    如何判断任务栈?

    通过activityManager.getRunningTasks(10),获取到当前进程的任务栈列表。其中,列表中保存的是每个任务栈的对象ActivityManager.RunningTaskInfo。

    其中getRunningTask(int maxNum)的入参,表示返回List的最大长度,一般会有两个任务栈(一为当前应用的任务栈,一为桌面启动的任务栈)

    ActivityManager.RunningTaskInfo :

    • numActivities 当前栈中Activity数量
    • baseActivity 栈底的Activity
    • topActivity 栈顶的Activity

    具体代码

    /**
     * 判断应用是否处于启动状态
     *
     * @param ctx         Content
     * @param packageName 要判断应用的 ApplicationId
     * @return Boolean true:进程活着并且应用的任务栈不为空; false:进程死亡或者应用的任务栈为空
     */
    public static boolean isAppAlive(Context ctx, String packageName) {
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    
        for (int i = 0; i < processInfos.size(); i++) {
            RunningAppProcessInfo processInfo = processInfos.get(i);
            Logs.d(TAG, "isAppAlive: i = " + i + ", processName = " + processInfo.processName);
            if (processInfos.get(i).processName.equals(packageName)) {
                List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(10);
                for (int j = 0; j < runningTasks.size(); j++) {
                    ActivityManager.RunningTaskInfo taskInfo = runningTasks.get(j);
                    Logs.i(TAG, "isAppAlive: i = " + j + ", taskInfo.baseActivity = " + taskInfo.baseActivity.getClassName() + ", taskInfo.topActivity = " + taskInfo.topActivity.getClassName());
                    Logs.i(TAG, "isAppAlive: i = " + j + ", taskInfo.numActivities = " + taskInfo.numActivities + ", taskInfo.numRunning = " + taskInfo.numRunning +
                            ", taskInfo.description = " + taskInfo.description + ", taskInfo.describeContents() = " + taskInfo.describeContents());
                    //在此处判断全限定类名时,应以包名为基准(当applicationId与packageName不一致时)
                    if (taskInfo.baseActivity.getClassName().startsWith(packageName) && taskInfo.numActivities > 0) {
                        Logs.i(TAG, "isAppAlive: " + String.format(Locale.CHINA, "the %s is running, isAppAlive return true.", packageName));
                        return true;
                    }
                }
            }
        }
        Logs.i(TAG, "isAppAlive: " + String.format(Locale.CHINA, "the %s is not running, isAppAlive return false.", packageName));
        return false;
    }
    

    场景日志

    当前应用处于前台运行中,应用的任务栈处于第一位

    isAppAlive: i = 0, taskInfo.baseActivity = com.jiedai.loan.ui.activity.HomeActivity, taskInfo.topActivity = com.jiedai.loan.ui.activity.LoginPasswordActivi
    isAppAlive: i = 0, taskInfo.numActivities = 3, taskInfo.numRunning = 3, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 1, taskInfo.baseActivity = com.meizu.flyme.launcher.Launcher, taskInfo.topActivity = com.meizu.flyme.launcher.Launcher
    isAppAlive: i = 1, taskInfo.numActivities = 1, taskInfo.numRunning = 1, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 0, processName = com.jiedai.loan
    isAppAlive: i = 1, processName = com.jiedai.loan:pushcore
    

    应用处于后台运行中(Home桌面状态),桌面启动任务栈处于第一位

    isAppAlive: i = 0, taskInfo.baseActivity = com.meizu.flyme.launcher.Launcher, taskInfo.topActivity = com.meizu.flyme.launcher.Launcher
    isAppAlive: i = 0, taskInfo.numActivities = 1, taskInfo.numRunning = 1, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 1, taskInfo.baseActivity = com.jiedai.loan.ui.activity.HomeActivity, taskInfo.topActivity = com.jiedai.loan.ui.activity.LoginPasswordActivity
    isAppAlive: i = 1, taskInfo.numActivities = 3, taskInfo.numRunning = 3, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 0, processName = com.jiedai.loan
    isAppAlive: i = 1, processName = com.jiedai.loan:pushcore
    

    应用的任务栈仅有一个Activity时,栈底与栈顶是同一个Activity。

    isAppAlive: i = 0, taskInfo.baseActivity = com.jiedai.loan.ui.activity.HomeActivity, taskInfo.topActivity = com.jiedai.loan.ui.activity.HomeActivity
    isAppAlive: i = 0, taskInfo.numActivities = 1, taskInfo.numRunning = 1, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 1, taskInfo.baseActivity = com.meizu.flyme.launcher.Launcher, taskInfo.topActivity = com.meizu.flyme.launcher.Launcher
    isAppAlive: i = 1, taskInfo.numActivities = 1, taskInfo.numRunning = 1, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 0, processName = com.jiedai.loan
    isAppAlive: i = 1, processName = com.jiedai.loan:pushcore
    

    应用进程活着,但是任务栈为空时,仅有一个桌面启动的任务栈

    isAppAlive: i = 0, taskInfo.baseActivity = com.meizu.flyme.launcher.Launcher, taskInfo.topActivity = com.meizu.flyme.launcher.Launcher
    isAppAlive: i = 0, taskInfo.numActivities = 1, taskInfo.numRunning = 1, taskInfo.description = null, taskInfo.describeContents() = 0
    isAppAlive: i = 0, processName = com.jiedai.loan
    isAppAlive: i = 1, processName = com.jiedai.loan:pushcore
    

    相关文章

      网友评论

          本文标题:判断应用是否运行以及Activity任务栈状态

          本文链接:https://www.haomeiwen.com/subject/kjeinctx.html