2

I have several services that are similar and implement a MyAbstractService abstract class.

public Service1 extends MyAbstractService {
  // ...
}

public Service2 extends MyAbstractService {
  // ...
}

public abstract MyAbstractService extends Service {
  // ...
}

Usually an activity binds to only one service.

But I have an activity that needs to bind to all of them. I am wondering whether I can use one ServiceConnection

public void onCreate() {
    bindService(intentService1, mConnection);
    bindService(intentService2, mConnection);
}

private ServiceConnection mConnection = new  ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MyAbstractService.LocalBinder binder = (MyAbstractService.LocalBinder) service;
        MyAbstractService svc = binder.getService();
                    # The activity keeps all connected services in a HashMap
        mServices.put(name, svc);
    }
};

Can I reuse the same mConnection or do I need to create one ServiceConnection per connected Service?

rds
  • 26,253
  • 19
  • 107
  • 134
  • Not the way you have 'ServiceConnection' using an abstract class for the 'LocalBinder'. You could probably check the 'ComponentName' and use some logic to bind the right 'Service'. What do the 'Service' have in common that you have created an abstract class? – techi.services Feb 28 '12 at 22:57
  • All services provide easy access to content from ContentProviders. And some ContentProviders share common columns. The main activity is a ListActivity that only display these common columns. Hence, the home activity needs to bind to all services to call the common data they provide. – rds Feb 28 '12 at 23:09
  • The only problem I can think of is if you try accessing member functions of either of the concrete services, since you will need to recast the class. Are you just going to have a set of common abstract member functions each concrete service will implement? Also, have you tried it? – techi.services Feb 29 '12 at 15:55
  • Yes, I have now finished this implementation (with two services, there will be a couple more in the future). What I don't want is to leak a ServiceConnection that the system wouldn't see because "another" connection has been properly cut. – rds Feb 29 '12 at 19:25
  • I don't access cast the services in the Main Activity, and I don't call specialized methods. – rds Feb 29 '12 at 19:27
  • The LogCat will tell you if you have leaked a `ServiceConnection` when you test thoroughly. TBH I have never bothered with binding an `Activity` to a `Service`, other than early days of experimenting. I have always implemented my own version of an `IntentService` and queue up requests via the message handler and use a `Messenger` to pass back data to the `Activity` handlers. – techi.services Feb 29 '12 at 20:37
  • Possible duplicate of [Is there a need to have one serviceConnection for each android service?](http://stackoverflow.com/questions/22256595/is-there-a-need-to-have-one-serviceconnection-for-each-android-service) – Sufian Oct 31 '16 at 11:18

0 Answers0