Android 判断某个Activity 是否在前台运行

    /**
     * 判断某个界面是否在前台
     *
     * @param context   Context
     * @param className 界面的类名
     * @return 是否在前台显示
     */
    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List list = am.getRunningTasks(1);
//        boolean flag=false;
        for (ActivityManager.RunningTaskInfo taskInfo : list) {
            if (taskInfo.topActivity.getShortClassName().contains(className)) { // 说明它已经启动了
//                flag = true;
                return true;
            }
        }
        return false;
    }


        if(TCPHandle.isForeground(HomeActivity.this,"EvaluateForHandActivity")||TCPHandle.isForeground(HomeActivity.this,"EvaluateActivity"))
        {
            return;
        }



你可能感兴趣的:(Android开发)