检查服务是从广播接收器运行接收器、是从

由网友(毁你半壁江山__)分享简介:我就想问一下,如果有可能来检查服务是从广播接收器运行。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 Ac...

我就想问一下,如果有可能来检查服务是从广播接收器运行。

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.

感谢您的precious帮助

Thanks for your precious help

推荐答案

是它可以检测,如果服务是从任何地方运行,你可用上下文目标:

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 是服务类。

阅读全文

相关推荐

最新文章