There is no API to find out how many customers are associated with the Service.
If you are implementing your own service, then in your ServiceConnection you can increase / decrease the reference counter to track the number of connected clients.
Below 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 {
public void onServiceConnected(ComponentName className, IBinder service) {
...
MyService.clientConnected();
Log.d("MyServiceConnection", "Client Connected! clients = " + MyService.getNumberOfBoundClients());
}
public void onServiceDisconnected(ComponentName className) {
...
MyService.clientDisconnected();
Log.d("MyServiceConnection", "Client disconnected! clients = " + MyService.getNumberOfBoundClients());
}
}
source
share