Android判断应用是否在前台运行
private int isFrontShow(Context context) {if (context == null) {return 0;}ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(20);if (context.getPackageName().equals(runningTasks.get(0).topActivity.getPackageName())) {return 1;} else {for (ActivityManager.RunningTaskInfo runningTask : runningTasks) {if (context.getPackageName().equals(runningTask.topActivity.getPackageName())) {return 2;}}}return 3;}