开发者

Android Service starts/binds correctly, but only the first time

开发者 https://www.devze.com 2023-01-09 13:36 出处:网络
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.

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 ar开发者_StackOverflow社区ound, 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


In my case problem was related to bindService(). I called it only once in onResume() - following some sample.

It seems that bindService() should be called each time you start service (with ContextCompat.startForegroundService() in my case).

0

精彩评论

暂无评论...
验证码 换一张
取 消