首页 > 代码库 > Android之判断当前指定App是否在前台

Android之判断当前指定App是否在前台

直接上代码,不多说

//在进程中去寻找当前APP的信息,判断是否在前台运行private boolean isAppOnForeground() {ActivityManager activityManager =(ActivityManager) getApplicationContext().getSystemService(   Context.ACTIVITY_SERVICE);String packageName =getApplicationContext().getPackageName();List<RunningAppProcessInfo>appProcesses = activityManager.getRunningAppProcesses();if (appProcesses == null)return false;for (RunningAppProcessInfo appProcess : appProcesses) {if (appProcess.processName.equals(packageName)       && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {   return true;}}return false;}

 

Android之判断当前指定App是否在前台