Android 服务正确启动/绑定,但仅限第一次

Ale*_*lex 5 android android-service

我有一个服务在第一次调用时可以正确启动和绑定,但是当被其他活动调用时,对同一服务的连续绑定失败。

编码:

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);
Run Code Online (Sandbox Code Playgroud)

第二次,在调用 之后activity.bindService,serviceconnection 的onServiceConnected方法永远不会被调用。我使用执行绑定的连接类,因此这两个活动的方法相同。该服务也正确添加了清单文件。

有任何想法吗?

非常感谢

l00*_*00k 0

就我而言,问题与bindService(). 我只在onResume()一些示例中调用过它一次。

似乎bindService()每次启动服务时都应该调用(ContextCompat.startForegroundService()在我的例子中)。