直接上代码,不多说
//在进程中去寻找当前APP的信息,判断是否在前台运行 private boolean isAppOnForeground() { ActivityManager activityManager =(ActivityManager) getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE); String packageName =getApplicationContext().getPackageName(); List<RunningAppProcessInfo>appProcesses = 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; }
时间: 2024-10-16 04:35:33