检查服务是否正在从广播接收器运行

use*_*646 7 service android broadcastreceiver

我想问一下,是否有可能检查服务是否正在从广播接收器运行.

我知道在Activity中这是可能的.

谢谢你的宝贵帮助

Tal*_*nel 8

是的'有可能检测到服务是否在您拥有可用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;
}
Run Code Online (Sandbox Code Playgroud)

MyService 是你的服务类.