无法取消注册Android中的广播接收器?

时间:2011-06-04 21:03:59

标签: android broadcastreceiver

我有一个广播接收器,我尝试在OnPause()方法中取消注册。但它似乎被阻止了,虽然我在OnResume()中注册它,但它并没有被激活。如果我只注册它,它运行良好,但后来我得到“泄漏广播接收器,你应该取消注册它”的例外,如下所示:

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 android.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 

有很多关于这个的帖子,我尝试了很多没有结果的帖子。实践是在OnResume中注册它并在OnPause中取消注册,对吧?或者我应该在onStop()中取消注册。我试过,然后它从不听。

感谢您的任何指示!

1 个答案:

答案 0 :(得分:3)

  

实践是在OnResume中注册它并在OnPause中取消注册,对吗?

是。 Here is a sample application证明了这一点。