I have a broadcast receiver that I try to unregister in the OnPause()
method. But then it gets blocked it seems, and although I register it in the OnResume()
, it doesn't fire. If I only register it it works well, but then I get the exception "leaking broadcast receiver, you should unregister it", as below:
06-04 22:50:04.267: ERROR/ActivityThread(4188): Activity com.example.android.CView has leaked IntentReceiver com.example.android.CView$1@4062b650 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-04 22:50:04.267: ERROR/ActivityThread(4188): android.app.IntentReceiverLeaked: Activity com.example.android.CView has leaked IntentReceiver com.example.android.CVieww$1@4062b650 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-04 22:50:04.267: ERROR/ActivityThread(4188): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:790)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:575)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:848)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at and开发者_开发知识库roid.app.ContextImpl.registerReceiver(ContextImpl.java:835)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at android.app.ContextImpl.registerReceiver(ContextImpl.java:829)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at cActivity com.example.android.BehaviourView.onStart(BehaviourView.java:230)
06-04 22:50:04.267: ERROR/ActivityThread(4188): at
There are tons of posts concerning this, and I have tried many of them without results. The practice is to register it in OnResume
and unregister it in the OnPause
, right? Or should I unregister it in onStop()
. I have tried and then it never listens.
Thanks for any pointers!
The practice is to register it in OnResume and unregister it in the OnPause, right?
Yes. Here is a sample application demonstrating this.
精彩评论