Android to determine whether the app is running in the foreground or in the background, directly read the code, can be used directly.
public static Boolean IsBackground (context context) {Activitymanager Activitymanager = (Activitymanager) Context.getsystemservice (Context.activity_service); list<runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses (); for ( Runningappprocessinfo appprocess:appprocesses) {if (AppProcess.processName.equals (Context.getpackagename ())) {/* background=400 empty=500 foreground=100gone=1000 perceptible=130 service=300 isible=200 */Log.i ( Context.getpackagename (), "This appimportace =" + appprocess.importance+ ", Context.getclass (). GetName () =" + Context.getclass (). GetName ()); if (appprocess.importance! = runningappprocessinfo.importance_foreground) {Log.i ( Context.getpackagename (), "in background" + Appprocess.processname); return true;} else {log.i (Context.getpackagename (), "in foreground" + Appprocess.processname); return false;}} return false;}
Android determines whether the app is running in the foreground or in the background