我针对这个问题找到了很多答案。但这都是关于单个活动的。如何检查整个应用程序是否在前台运行?
我不明白你想要什么,但你可以检测当前的前台/后台应用程序ActivityManager.getRunningAppProcesses()
call.
就像是,
class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {
@Override
protected Boolean doInBackground(Context... params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}
// Use like this:
boolean foregroud = new ForegroundCheckTask().execute(context).get();
如果我理解错了也请告诉我..
UPDATE:看看这个问题从后台任务或服务确定当前前台应用程序 https://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service提前了解更多信息..
Thanks..
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)