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

问题描述 投票:4回答:1

我有一个服务,它在首次调用时会启动并正确绑定,但是在其他活动调用时,对该服务的连续绑定会失败。

代码:

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);

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

有什么想法吗?

非常感谢

android android-service
1个回答
0
投票

就我而言,问题与bindService()有关。我在onResume()中只调用了一次-跟随一些示例。

[似乎每次启动服务时都应调用bindService()(在我的情况下为ContextCompat.startForegroundService())。

© www.soinside.com 2019 - 2024. All rights reserved.