如何判断一个Android服务是否在前台运行? [英] How to determine if an Android Service is running in the foreground?
本文介绍了如何判断一个Android服务是否在前台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为有一项服务在前台运行,如何检查我的实现是否有效?
I have a service which I believe to have running in the foreground, How do I check if my implementation is working?
推荐答案
private boolean isServiceRunning(String serviceName){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
if(runningServiceInfo.foreground)
{
//service run in foreground
}
}
}
return serviceRunning;
}
如果您想知道您的服务是否在前台运行,只需打开其他一些胖应用程序,然后检查服务是否仍在运行或仅检查标志 service.foreground
.
If you want to know if your service is running in foreground just open some others fat applications and then check if service is still running or just check flag service.foreground
.
这篇关于如何判断一个Android服务是否在前台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文