我想问一下是否可以从广播接收器检查服务是否正在运行。
我知道在Activity中有可能。
感谢您的宝贵帮助
是的,可以从您有可用Context
对象的任何位置检测服务是否正在运行:
private boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
MyService
是您的服务等级。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句