判断App是否在前台

##方法一:ActivityLifecycleCallbacks ##html

Manifest.xmljava

<application
	android:label="@string/app_name"
	android:theme="@style/AppTheme"
	android:name=".MyApplication">

MyApplication.javaandroid

class MyApplication extends Application {
	public void onCreate(){
		Foreground.init(this);
	}
}

Foreground.javagit

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Usage:
 * 
 * 1. Get the Foreground Singleton, passing a Context or Application object unless you
 * are sure that the Singleton has definitely already been initialised elsewhere.
 * 
 * 2.a) Perform a direct, synchronous check: Foreground.isForeground() / .isBackground()
 * 
 * or
 * 
 * 2.b) Register to be notified (useful in Service or other non-UI components):
 * 
 *   Foreground.Listener myListener = new Foreground.Listener(){
 *       public void onBecameForeground(){
 *           // ... whatever you want to do
 *       }
 *       public void onBecameBackground(){
 *           // ... whatever you want to do
 *       }
 *   }
 * 
 *   public void onCreate(){
 *      super.onCreate();
 *      Foreground.get(this).addListener(listener);
 *   }
 * 
 *   public void onDestroy(){
 *      super.onCreate();
 *      Foreground.get(this).removeListener(listener);
 *   }
 */
public class Foreground implements Application.ActivityLifecycleCallbacks {

	public static final long CHECK_DELAY = 500;
	public static final String TAG = Foreground.class.getName();

	public interface Listener {

		public void onBecameForeground();

		public void onBecameBackground();

	}

	private static Foreground instance;

	private boolean foreground = false, paused = true;
	private Handler handler = new Handler();
	private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
	private Runnable check;

	/**
	 * Its not strictly necessary to use this method - _usually_ invoking
	 * get with a Context gives us a path to retrieve the Application and
	 * initialise, but sometimes (e.g. in test harness) the ApplicationContext
	 * is != the Application, and the docs make no guarantees.
	 *
	 */
	public static Foreground init(Application application){
		if (instance == null) {
			instance = new Foreground();
			application.registerActivityLifecycleCallbacks(instance);
		}
		return instance;
	}

	public static Foreground get(Application application){
		if (instance == null) {
			init(application);
		}
		return instance;
	}

	public static Foreground get(Context ctx){
		if (instance == null) {
			Context appCtx = ctx.getApplicationContext();
			if (appCtx instanceof Application) {
				init((Application)appCtx);
			}
			throw new IllegalStateException(
				"Foreground is not initialised and " +
				"cannot obtain the Application object");
		}
		return instance;
	}

	public static Foreground get(){
		if (instance == null) {
			throw new IllegalStateException(
				"Foreground is not initialised - invoke " +
				"at least once with parameterised init/get");
		}
		return instance;
	}

	public boolean isForeground(){
		return foreground;
	}

	public boolean isBackground(){
		return !foreground;
	}

	public void addListener(Listener listener){
		listeners.add(listener);
	}

	public void removeListener(Listener listener){
		listeners.remove(listener);
	}

	@Override
	public void onActivityResumed(Activity activity) {
		paused = false;
		boolean wasBackground = !foreground;
		foreground = true;

		if (check != null)
			handler.removeCallbacks(check);

		if (wasBackground){
			Log.i(TAG, "went foreground");
			for (Listener l : listeners) {
				try {
					l.onBecameForeground();
				} catch (Exception exc) {
					Log.e(TAG, "Listener threw exception!", exc);
				}
			}
		} else {
			Log.i(TAG, "still foreground");
		}
	}

	@Override
	public void onActivityPaused(Activity activity) {
		paused = true;

		if (check != null)
			handler.removeCallbacks(check);

		handler.postDelayed(check = new Runnable(){
			@Override
			public void run() {
				if (foreground && paused) {
					foreground = false;
					Log.i(TAG, "went background");
					for (Listener l : listeners) {
						try {
							l.onBecameBackground();
						} catch (Exception exc) {
							Log.e(TAG, "Listener threw exception!", exc);
						}
					}
				} else {
					Log.i(TAG, "still foreground");
				}
			}
		}, CHECK_DELAY);
	}

	@Override
	public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

	@Override
	public void onActivityStarted(Activity activity) {}

	@Override
	public void onActivityStopped(Activity activity) {}

	@Override
	public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

	@Override
	public void onActivityDestroyed(Activity activity) {}
}

##方法二 ##github

AndroidManifest.xmlapp

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

java fileless

private boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }

    return isInBackground;
}

参考:ide

方法一	http://steveliles.github.io/is_my_android_app_currently_foreground_or_background.html
方法一另外一个实现	http://www.cnblogs.com/stay/p/5012370.html
方法二	http://tips.androidhive.info/2015/04/android-how-to-check-if-the-app-is-in-background-or-foreground/