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) &¤tpackagename.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