判断一个app运行在foreground

Android 判断程序前后台状态 ,直接上代码

public static boolean isAppOnForeground(Context context) {  
     ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
     List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();  
     if (appProcesses == null) {  
       return false;  
     }  
     final String packageName = context.getPackageName();  
     for (RunningAppProcessInfo appProcess : appProcesses) {  
       if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {  
         return true;  
       }  
     }  
     return false;  
 }  

你可能感兴趣的:(判断一个app运行在foreground)