//在進程中去尋找當前APP的信息,判斷是否在前臺運行 private boolean isAppOnForeground() { ActivityManager activityManager =(ActivityManager) getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE); String packageName =getApplicationContext().getPackageName(); ListappProcesses = 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; }