我正在尝试构建一个简单的Android应用程序,如果当前没有应用程序在前台/可见屏幕上运行(除了我自己的应用程序),它将显示某种弹出窗口。那么如何检测屏幕上当前是否没有正在运行的应用程序呢?
提前致谢...
答案 0 :(得分:0)
您可以使用以下内容获取前台应用:
ActivityManager activityManager = (ActivityManager) newContext.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for(RunningAppProcessInfo appProcess : appProcesses){
if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
Log.i("Foreground App", appProcess.processName);
}
}
然后只需将appProcess.processName
与设备上启动器的包名称进行比较即可:
private String findLauncherPackageName(){
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
final ResolveInfo res = getPackageManager().resolveActivity(intent, 0);
Log.e("ANDRO_ASYNC", String.format("findLauncherPackageName()%s",res.activityInfo.packageName));
return res.activityInfo.packageName;
}