0
我绑定到一个服务在我的活动:为什么unregisterReceiver()永远不会调用onServiceDisconnected?
override fun onStart() {
Timber.d("onStart")
super.onStart()
val intent = Intent(this, MyService::class.java)
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
}
迄今工作。然后bindService()
电话onServiceConnected()
:
/** Defines callbacks for service binding, passed to bindService() */
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName,
service: IBinder) {
// We've bound to MyService, cast the IBinder and get MyService instance
val binder = service as MyService.MyBinder
myService = binder.service
isBound = true
registerReceiver(myBroadcastReceiver, filter)
}
override fun onServiceDisconnected(arg0: ComponentName) {
myService!!.removeRecevier([email protected])
isBound = false
unregisterReceiver(myBroadcastReceiver)
}
}
在我onStop()
我也有unbindService(serviceConnection)
,但这里的onServiceDisconnected()永远不会触发? 我在做什么错?
所以我得到:
Activity MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
你需要什么'myBroadcastReceiver'?为什么要在绑定到您的服务后注册? – pskink
该服务与收件人无关,我需要两者。我只是写了它,因为错误消息。 –
所以注册到'onStart'并注销'onStop'(或'onCreate' /'onDestroy') – pskink