//在进程中去寻找当前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-07-30 13:49:36 发布