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 BooleanIsforeground (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 be based on the different importance to judge the foreground or background runningappprocessinfo inside the constant imoportance is the above mentioned front desk backstage, in fact imoportance is the importance of this app process, Because the system recycles, the process is recycled according to the imoportance.    You can read the document in detail.  Public Static Final intImportance_background = 400//Backstage     Public Static Final intImportance_empty = 500//Empty Process     Public Static Final intImportance_foreground = 100//Onresume (), which can be understood as the activity life cycle, at the front of the screen, with access to the focus;     Public Static Final intImportance_service = 300//in the service     Public Static Final intimportance_visible = 200//OnStart (), which can be understood as the activity life cycle at the front of the screen, is not getting the focus;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 BooleanIsforeground (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 above, you can use Activitylifecycleif(Build.VERSION.SDK_INT >= 14) {Lifecycle=Newsimpleactivitylifecycle (); Registeractivitylifecyclecallbacks (lifecycle); Public classSimpleactivitylifecycleImplementsApplication.activitylifecyclecallbacks {Private BooleanIsforeground =false;//whether the app is in the front@Override Public voidonactivitycreated (activity activity, Bundle savedinstancestate) {} @Override Public voidonactivitystarted (activity activity) {} @Override Public voidonactivityresumed (activity activity) {Isforeground=true; } @Override Public voidonactivitypaused (activity activity) {Isforeground=false; } @Override Public voidonactivitystopped (activity activity) {} @Override Public voidonactivitysaveinstancestate (activity activity, Bundle outstate) {} @Override Public voidonactivitydestroyed (activity activity) {} Public BooleanIsforeground () {returnIsforeground; }}

Android to determine if the app is running in the foreground

Related Article

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.