美文网首页
Android 判断当前app是否是在后台运行

Android 判断当前app是否是在后台运行

作者: 我爱爬爬虾 | 来源:发表于2020-01-09 09:15 被阅读0次

    /**

    * 判断程序是否在后台运行

    *

    * @param activity

    * @return true 表示在后台运行

    */

    public static boolean isRunBackground(Activity activity) {

        ActivityManager activityManager = (ActivityManager) activity.getApplicationContext()

                .getSystemService(Context.ACTIVITY_SERVICE);

        String packageName = activity.getApplicationContext().getPackageName();

        //获取Android设备中所有正在运行的App

        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager

                .getRunningAppProcesses();

        if (appProcesses == null)

            return true;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {

            // The name of the process that this object is associated with.

            if (appProcess.processName.equals(packageName)

                    && appProcess.importance ==

                    ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {

                return false;

            }

        }

        return true;

    }

    记下来  以后好复制

    相关文章

      网友评论

          本文标题:Android 判断当前app是否是在后台运行

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