Check if Service is running from a Broadcast Receiver

风格不统一 提交于 2019-12-04 01:13:33

问题


I'll like to ask if it's possible to check a service is running from Broadcast receiver.

I know that it's possible in Activity.

Thanks for your precious help


回答1:


yes' it's possible to detect if service is running from anywhere you have available Context object:

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 is your service class.



来源:https://stackoverflow.com/questions/22511018/check-if-service-is-running-from-a-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!