代码
/**
* 判断某个activity是否在前台显示
*/
public static boolean isForeground(Activity activity) {
return isForeground(activity, activity.getClass().getName());
}
/**
* 判断某个界面是否在前台,返回true,为显示,否则不是
*/
static boolean isForeground(Activity 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.isEmpty()) {
ComponentName cpn = list.get(0).topActivity;
return cpn != null && className.equals(cpn.getClassName());
}
return false;
}
调用
if (isForeground(activity)){
//在前台显示,做逻辑
}
评论 (0)