欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

判断应用是否处于前台

程序员文章站 2022-05-03 12:08:07
private boolean isForeground() { ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List
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;
}