2017-10-13 5 views
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()?

+0

'myBroadcastReceiver'에 무엇이 필요합니까? 왜 당신은 당신의 서비스에 바인딩 한 후에 그것을 등록하기를 원합니 까? – pskink

+0

그 서비스는 수신기와 아무런 관련이 없습니다. 둘 다 필요합니다. 오류 메시지로 인해 썼습니다. –

+0

그래서'onStart'에 등록하고'onStop' (또는'onCreate' /'onDestroy')에 등록 해제하십시오 – pskink

답변

0

당신은 서비스에 대한 연결이 손실되었을 때 호출 onServiceDisconnected()

의 목적을 오해하고 있습니다. 이는 일반적으로 서비스를 호스팅하는 프로세스가 손상되었거나 종료되었을 때 발생합니다. ServiceConnection 자체는 제거되지 않습니다.이 서비스에 대한 바인딩은 활성 상태로 유지되며 서비스가 다음에 실행될 때 onServiceConnected(ComponentName, IBinder)에 대한 호출을 받게됩니다.

실제 서비스에 대한 연결이 끊어지면 실제로 호출됩니다. 서비스에서 바인드 해제 할 때 수동으로 정리해야합니다. onStart()/onStop()처럼 ServiceConnection과 다른 (다른) 레벨로 수신기를 처리하는 것이 좋습니다.

관련 문제