2

I want to create a single button which serves both functions of Starting and Stopping a service.

Also I want to make sure that even if user quits the application and again comes back then according to whether service is running or not, I want to show appropriate Text on Button.

So in short, With what thing I can get to know if service is running or not ?

Harshit Syal
  • 649
  • 2
  • 11
  • 22
  • possible duplicate of [android: check if a service is running](http://stackoverflow.com/questions/600207/android-check-if-a-service-is-running) – FoamyGuy Jun 30 '12 at 15:53

1 Answers1

3

You can check if service is running or not with below code. Rest of the question is logic based once you find the service is running or not.

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.example.MyService".equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

I refer this answer just check it

Community
  • 1
  • 1
rajpara
  • 5,203
  • 1
  • 29
  • 46