Android服务正确启动/绑定(bind),但只是第一次

标签 android android-service

我有一个服务,它在第一次被调用时启动并正确绑定(bind),但是当被其他 Activity 调用时,对同一服务的连续绑定(bind)失败。

编码:

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 之后,服务连接的onServiceConnected方法永远不会被调用。我使用一个连接类来进行绑定(bind),所以这两个 Activity 的方法是相同的。该服务还正确添加了 list 文件。

有任何想法吗?

非常感谢

最佳答案

在我的情况下,问题与 bindService() 有关.
我在 onResume() 中只调用过一次- 跟随一些样本。

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

关于Android服务正确启动/绑定(bind),但只是第一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3380923/

相关文章:

java - 带有圆角的 AppWidget 图像

android - BoundService + LiveData + ViewModel 新安卓推荐架构最佳实践

android,提供导航服务

java - 使用 Android 前台服务为 MediaPlayer 创建通知

带有 EventBus 和启动服务的 Android MVP

android - 无法在 Windows 8 64 位下安装 Android Studio

android,这是使用 MyLocationOverlay 获取用户位置以将其发送到服务器的正确方法吗...?

android - 深度测试无法与模板测试一起正常工作

Android M - 无法检查运行时权限

android - 将 LocationManager 作为 Android 服务启动