如果在之后使用服务函数,则不会调用onServiceConnected

问题描述

我在项目中使用bindingservice。我需要实现它,以便在单击启动服务的按钮时进行绑定。如果我在onCreate()中使用绑定,则可以正常工作(但与我需要的不匹配)。而且,如果您向onClick添加绑定,然后与服务进行交互,则onServiceConnected不起作用()

清单:

    <service
        android:name="com.example.gotracker.LocationService"
        android:enabled="true"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" />

意图:

intentService = Intent(activity?.applicationContext,LocationService::class.java)

ServiceConnection:

        connection = object : ServiceConnection {
            private lateinit var locationBinder: LocationService.LocationServiceBinder
            override fun onServicedisconnected(name: ComponentName?) {
                LocationService.isBound = false
            }

            override fun onServiceConnected(name: ComponentName?,service: IBinder?) {

                if (!::locationService.isInitialized) {
                    locationBinder =
                        service as LocationService.LocationServiceBinder
                    locationService = locationBinder.getLocationService()
                    Log.d("connection","onServiceConnected")
                }
            }

        }

从onClick调用功能

private fun dobindService() {

    activity?.applicationContext?.bindService(
        intentService,connection,Context.BIND_AUTO_CREATE
    )
     LocationService.isBound = true
}

最后:“ kotlin.UninitializedPropertyAccessException:lateinit属性locationService尚未初始化”

在我看来,由于某种原因,服务方法调用速度快于绑定速度。该如何解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)