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