0

I found some code that tells you if your service is running. But it is getting compile errors and I don't know why. My service class name is AudioService (the class that extends Service) and I am getting compile error on the if statement line of code where it says,

AudioService.equals(service.service.getClassName()) and the error message from the compiler is,

Cannot make a static reference to the non-static method equals(object) from type object

I am using the method inside of the Activity that is bound to the service. I call bindService() in the onResume method and unbindService() in the onPause. Just want to use some method to make a Toast message to show that the service is actually running.

What am I doing wrong and how to get this code working?

private boolean isServiceRunning() {
    ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (AudioService.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
1
  • please paste your stacktrace
    – dd619
    Dec 11, 2012 at 12:03

2 Answers 2

0

Use this AudioService.class.getName()

 private boolean isServiceRunning() {
  ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
  for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if (AudioService.class.getName().equals(service.service.getClassName())) {
      return true;
    }
  }
  return false;
}
0

Try this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.