0

I have a service A that needs to be notified about another service B. I bind service B to service A by calling following method that should not start the service , as stated in this question.

 serviceA.bindService(new Intent(serviceA, ServiceB.class), conn, 0);

However when I want to check if service is running following method return true even if service is just bound.

public static boolean isServiceRunning(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (ServiceB.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

Why does isServiceRunning return true for service that is just bound and not running (I checked that service is not running by looking into running apps in settings) ?

Community
  • 1
  • 1
jellyfication
  • 1,595
  • 1
  • 16
  • 37
  • can you verify that your `ServiceB#onCreate` is called or not ? – kiruwka Apr 01 '14 at 12:32
  • `ServiceB#onCreate`is not called. – jellyfication Apr 01 '14 at 12:39
  • well, than `ServiceB` is not created as you would expect. I guess `isServiceRunning` method code is checking it wrong – kiruwka Apr 01 '14 at 12:40
  • Suppling bindService with zero flag seems like a hack but gets the jobs done. My problem is I am unable to distinguish such service from running services. – jellyfication Apr 01 '14 at 12:42
  • I don't think you could do that. You need your application logic to remember that you have bound to the service, this how you will know it. You will also know when your service is started -> your `onServiceConnected` callback will be called at that point. – kiruwka Apr 01 '14 at 12:45
  • Well that is unfortunate, because serviceB is relying on `isServiceRunning` and when i bind my service I alter the state in ServiceB. – jellyfication Apr 01 '14 at 12:51
  • are you sure that your service is not actually running ? Can you put a breakpoint in your `ServiceB#onCreate` and also `onBind` and verify it is never called before making further assumptions ? – kiruwka Apr 01 '14 at 13:07

1 Answers1

-1

When you bound a service it is started by the system, bindservice() actually starts the service.

A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it. To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

Check Android document for more.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34