Android to determine if the app is running in the foreground

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.