Marcello
Marcello

Reputation: 201

How do I unbind from a specific service?

I have a bound service that in turn binds multiple services using the same ServiceConnection object. In the onServiceConnected I save the ComponentName and the Binder of each service inside a Map, so that I can use them individually. At a certain point I'd like to unbind some of these services separately. Is there a way to do this in Android?

The only way I was able to find out to unbind a service is to use unbindService(ServiceConnection), but I don't think that I can unbind a specific service using that.

Why this seems to be not supported? Are there any downsides?

Upvotes: 1

Views: 844

Answers (2)

Despite the time elapsed by the library and for offering support to lower versions in Android, it works correctly, I am also using services nested in a main one and the connections work or go through the main one. They are multiple Good note that when you start a connection via intent you must close it In the same way, it creates problems in the management of processes when the service is reused by other processes within the application. When closing the connection and instantiating a new one, it creates a small error in the process that was not terminated correctly. Just to mention how it is currently done! happy code!

Upvotes: 1

khurram
khurram

Reputation: 1362

here is sample code to bind and unbind service

i = new Intent(this, YourService.class)

protected ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
    Log.d(LOG_TAG, "onServiceConnected");
}

@Override
public void onServiceDisconnected(ComponentName name) {
    Log.d(LOG_TAG, "onServiceDisconnected");
}
}

public void start() {

mContext.bindService(i, mServerConn, Context.BIND_AUTO_CREATE);
mContext.startService(i);
}

public void stop() {
mContext.stopService(new Intent(mContext, ServiceRemote.class));
mContext.unbindService(mServerConn);
}

Upvotes: 0

Related Questions