判斷應用是否處於前臺

来源:https://www.cnblogs.com/xiaodidi/archive/2019/11/27/11944464.html

private boolean isForeground() { ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager. ...


private boolean isForeground() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();
    
    if (processes == null || processes.isEmpty()) {
        return false;
    }
    
    for (ActivityManager.RunningAppProcessInfo info : processes) {
        if (info.processName.equals(getPackageName()) &&
                info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}

 


您的分享是我們最大的動力!

更多相關文章
一周排行
x