Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Whether the current app is in the foreground
Private Boolean Isforeground (context context) {
if (context! = null) {
Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);
list<activitymanager.runningappprocessinfo> processes = activitymanager.getrunningappprocesses ();
for (Activitymanager.runningappprocessinfo processinfo:processes) {
if (ProcessInfo.processName.equals (Context.getpackagename ())) {
if (processinfo.importance = = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
}
}
return false;
}
Can judge the foreground or the backstage according to the different importance
Runningappprocessinfo inside the constant imoportance is the above mentioned front desk backstage, in fact imoportance is to indicate the importance of this app process, because the system is recycled, will be based on Imoportance to reclaim the process. You can read the document in detail.
public static final int importance_background = 400//background
public static final int importance_empty = 500//empty process
public static final int importance_foreground = 100//at the front of the screen, accessible to the focus can be understood as the activity life cycle of onresume ();
public static final int importance_service = 300//in service
public static final int importance_visible = 200//at the front of the screen, not getting the focus can be understood as onstart () of the activity life cycle;
But the above method some models do not support, the following method is still more reliable, but requires a permission
< uses-permission Android:name = "Android.permission.GET_TASKS"/>
Whether the current app is in the foreground
Private Boolean Isforeground (context context) {
if (context! = null) {
Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service);
ComponentName cn = Am.getrunningtasks (1). Get (0). topactivity;
String currentpackagename = Cn.getpackagename ();
if (! Textutils.isempty (currentpackagename) && currentpackagename.equals (Context.getpackagename ())) {
return true;
}
return false;
}
return false;
}
Version 4.0 or higher, you can use Activitylifecycle
if (Build.VERSION.SDK_INT >= 14) {
Lifecycle = new Simpleactivitylifecycle ();
Registeractivitylifecyclecallbacks (lifecycle);
public class Simpleactivitylifecycle implements Application.activitylifecyclecallbacks {
Private Boolean isforeground = false;//app is in front
@Override
public void onactivitycreated (activity activity, Bundle savedinstancestate) {
}
@Override
public void onactivitystarted (activity activity) {
}
@Override
public void onactivityresumed (activity activity) {
Isforeground = true;
}
@Override
public void onactivitypaused (activity activity) {
Isforeground = false;
}
@Override
public void onactivitystopped (activity activity) {
}
@Override
public void onactivitysaveinstancestate (activity activity, Bundle outstate) {
}
@Override
public void onactivitydestroyed (activity activity) {
}
public Boolean isforeground () {
return isforeground;
}
}
Android to determine if the app is running in the foreground