判断应用是否处于前台
程序员文章站
2022-08-02 19:13:43
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; }