Android之判断当前指定App是否在前台

直接上代码,不多说

//在进程中去寻找当前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;
}
上一篇:我的Android进阶之旅------>报 error: Apostrophe not preceded by \ 的错误解决办法


下一篇:数据库设计三大范式