如何检查服务是否运行在Android上? [英] How to check if a service is running on Android?

查看:159
本文介绍了如何检查服务是否运行在Android上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查,如果后台服务(在Android)运行?

How do I check if a background service (on Android) is running?

我想要切换服务的状态一个Android的活动 - 它让我打开它,如果它是关闭的,关闭,如果它是在

I want an Android activity that toggles the state of the service -- it lets me turn it on if it is off and off if it is on.

推荐答案

我不久前有同样的问题。由于我的服务是本地的,我最终只使用在服务类中的静态字段来切换状态,所描述的hackbod <一href="http://groups.google.com/group/android-developers/browse_thread/thread/8c4bd731681b8331/bf3ae8ef79cad75d">here

I had the same problem not long ago. Since my service was local, I ended up simply using a static field in the service class to toggle state, as described by hackbod here

修改(备案):

下面是该解决方案提出了hackbod:

Here is the solution proposed by hackbod:

如果您的客户端和服务器code是相同的.apk文件的一部分,你是   一个具体意图绑定到服务(一个指定的   准确的服务类),那么你可以简单地让您的服务设置   当它运行您的客户端可以查询全局变量。

If your client and server code is part of the same .apk and you are binding to the service with a concrete Intent (one that specifies the exact service class), then you can simply have your service set a global variable when it is running that your client can check.

我们故意没有一个API来检查服务是否   运行,因为,几乎没有失败,当你想要做的事   这样你最终的竞争条件在code。

We deliberately don't have an API to check whether a service is running because, nearly without fail, when you want to do something like that you end up with race conditions in your code.

这篇关于如何检查服务是否运行在Android上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆