In an Android service, is there a way to determine how many clients are bound to it?
3 Answers
There's no API to find out how many clients are bound to a Service.
If you are implementing your own service, then in your ServiceConnection you can increment/decrement a reference count to keep track of the number of bound clients.
Following is some psudo code to demonstrate the idea :
MyService extends Service {
...
private static int sNumBoundClients = 0;
public static void clientConnected() {
sNumBoundClients++;
}
public static void clientDisconnected() {
sNumBoundClients--;
}
public static int getNumberOfBoundClients() {
return sNumBoundClients;
}
}
MyServiceConnection extends ServiceConnection {
// Called when the connection with the service is established
public void onServiceConnected(ComponentName className, IBinder service) {
...
MyService.clientConnected();
Log.d("MyServiceConnection", "Client Connected! clients = " + MyService.getNumberOfBoundClients());
}
// Called when the connection with the service disconnects
public void onServiceDisconnected(ComponentName className) {
...
MyService.clientDisconnected();
Log.d("MyServiceConnection", "Client disconnected! clients = " + MyService.getNumberOfBoundClients());
}
}

- 12,711
- 1
- 37
- 32
-
2+1 However, this only works if you have a local service running in the same process as your clients. It doesn't work if your service is running in a remote process and it also doesn't work if you offer your service to multiple clients that are not part of your application. – David Wasser Aug 07 '12 at 18:27
-
1David is right, my example will only work for a Local service. – Akos Cz Aug 08 '12 at 00:44
-
I haven't had the need to implement a RemoteService yet, so I'm not sure how the RemoteCallback list would be used to make my example work with a RemoteService. – Akos Cz Aug 08 '12 at 00:53
There doesn't seem to be an easy, standard way to do this. I can think of 2 ways. Here's the simple way:
Add a call to your service's API like disconnect()
. The client should call disconnect()
before it calls unbindService()
. Create a member variable in the service like private int clientCount
to keep track of the number of bound clients. Track the number of bound clients by incrementing the count in onBind()
and decrementing it in disconnect()
.
The complicated way involves implementing a callback interface from your service to the clients and using RemoteCallbackList
to determine how many clients are actually bound.

- 93,459
- 16
- 209
- 274
You can keep track of the connected clients by overriding onBind()
(increase count), onUnbind()
(decrease count and return true
) and onRebind()
(increase count).

- 1,847
- 1
- 17
- 15
-
1According to [this](https://groups.google.com/forum/#!msg/android-developers/2IegSgtGxyE/iXP3lBCH5SsJ), `onBind()` is called once for the first request and a cached `IBinder` is returned by the system on subsequent requests without bothering the service. The documentation regarding this issue is incorrect. – Daniel Feb 12 '15 at 16:13