我想问问是否可以检查广播接收器正在运行的服务。
我知道这在 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
是您的服务类。
关于android - 检查服务是否正在从广播接收器运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22511018/