首页 > 代码库 > 判断android应用是否在运行等方式

判断android应用是否在运行等方式

/***     * 判断应用当前是否前台运行     *      * @return     */    public static boolean isTopRun(Context context) {        Boolean isTopRun = false;        try {            /* 判断当前程序是不在前台显示 */            ActivityManager activityManager = (ActivityManager) context                    .getSystemService(Context.ACTIVITY_SERVICE);            List<RunningTaskInfo> tasksInfo = activityManager                    .getRunningTasks(1);            if (tasksInfo.size() > 0) {                String packageName = tasksInfo.get(0).topActivity                        .getPackageName();                if (!TextUtils.isEmpty(packageName)                        && packageName.startsWith(getAppPackageName())) {                    isTopRun = true;                }            }        } catch (Exception e) {            e.printStackTrace();        }        return isTopRun;    }
/***     * 判断应用是否在运行     *      * @return     */    public static boolean isAppRunning(Context context){        boolean isAppRunning = false;        try {            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);            List<RunningTaskInfo> list = activityManager.getRunningTasks(100);                        String pageName = "com.example.packagename";            pageName = getAppPackageName();            for (RunningTaskInfo info : list) {                if (info.topActivity.getPackageName().equals(pageName) || info.baseActivity.getPackageName().equals(pageName)) {                isAppRunning = true;                Log.i("SystemUtils",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());                break;                }                }        } catch (Exception e) {            e.printStackTrace();        }                return isAppRunning;    }
/***     * 判断应用是否在运行     *      * @return     */    public static boolean isAppRunning(Context context){        boolean isAppRunning = false;        try {            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);            List<RunningTaskInfo> list = activityManager.getRunningTasks(100);                        String pageName = "com.example.packagename";            pageName = getAppPackageName();            for (RunningTaskInfo info : list) {                if (info.topActivity.getPackageName().equals(pageName) || info.baseActivity.getPackageName().equals(pageName)) {                isAppRunning = true;                Log.i("SystemUtils",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());                break;                }                }        } catch (Exception e) {            e.printStackTrace();        }                return isAppRunning;    }

 

判断android应用是否在运行等方式