android 如何判斷應用在前臺
阿新 • • 發佈:2018-12-09
private boolean isAppOnForeground() { ActivityManager activityManager = (ActivityManager) mContext.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); String packageName = mContext.getApplicationContext().getPackageName(); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; }