views:

40

answers:

1

Hi all, I have a service that starts and binds correctly when it's first called, but successive bindings to that same service fail when called by other activities.

The code:

activity.startService(new Intent().setClass(activity, ServerListenerService.class));        

xmppServiceConnection = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName name) {
            ServerActivityConnection.this.xmppService = null;
        }

        public void onServiceConnected(ComponentName name, IBinder binder) {
            //set everything up
        }
    };

activity.bindService(new Intent().setClass(activity, ServerListenerService.class), xmppServiceConnection, Activity.BIND_AUTO_CREATE);

The second time around, after the call to activity.bindService, the serviceconnection's onServiceConnected method never gets called. I use a connection class that does the binding, so the method is the same for both activities. The service is also correctly added the manifest file.

Any ideas?

Many Thanks

+1  A: 

Make sure you unbind before you try binding again with the same ServiceConnection.

Also, you typically do not need both startService() and bindService(). You NEVER need both startService() and bindService(..., BIND_AUTO_CREATE). Unless you have a very specific reason you need to use startService() as well as bindService(), I would only use one.

CommonsWare