我想问一下是否可以从广播接收器中检查服务是否正在运行。我知道在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
是你的服务类。