androidandroid-service

How to check if a service is running on Android?


How do I check if a background service is running?

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.


Solution

  • 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

    EDIT (for the record):

    Here is the solution proposed by hackbod:

    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.

    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.