为什么 unregisterReceiver() 从不调用 onServiceDisconnected?
Why unregisterReceiver() never calls onServiceDisconnected?
我在 Activity:
中绑定了一个服务
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(this@MainActivity)
isBound = false
unregisterReceiver(myBroadcastReceiver)
}
}
在我的 onStop()
我也有 unbindService(serviceConnection)
,但是这里永远不会触发 onServiceDisconnected() ? 我做错了什么?
因此我得到:
Activity MainActivity has leaked IntentReceiver com.example.MyBroadcastReceiver@68091a2 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity MainActivity has leaked IntentReceiver com.example.MyBroadcastReceiver@68091a2 that was originally registered here. Are you missing a call to unregisterReceiver()?
的目的
Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to onServiceConnected(ComponentName, IBinder)
when the Service is next running.
所以这实际上只会在您失去与实际服务的连接时调用。当您与服务解除绑定时,您必须手动进行清理。在与 ServiceConnection
不同的(其他)级别上更好地处理接收器 - 例如 onStart()
/onStop()
.
我在 Activity:
中绑定了一个服务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(this@MainActivity)
isBound = false
unregisterReceiver(myBroadcastReceiver)
}
}
在我的 onStop()
我也有 unbindService(serviceConnection)
,但是这里永远不会触发 onServiceDisconnected() ? 我做错了什么?
因此我得到:
Activity MainActivity has leaked IntentReceiver com.example.MyBroadcastReceiver@68091a2 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity MainActivity has leaked IntentReceiver com.example.MyBroadcastReceiver@68091a2 that was originally registered here. Are you missing a call to unregisterReceiver()?
Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to
onServiceConnected(ComponentName, IBinder)
when the Service is next running.
所以这实际上只会在您失去与实际服务的连接时调用。当您与服务解除绑定时,您必须手动进行清理。在与 ServiceConnection
不同的(其他)级别上更好地处理接收器 - 例如 onStart()
/onStop()
.