判断Activty是否在前台运行

/**
     * 判断某个界面是否在前台
     *
     * @param context
     * @param className   某个界面名称
     *
     */
    public static boolean isActivityForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className)) {
            return false;
        }

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;
            }
        }

        return false;
    }

 

posted on 2019-05-29 16:19  LoaderMan  阅读(331)  评论(0编辑  收藏  举报

导航