2011-06-04 27 views
0

我有一個廣播接收器,我嘗試在OnPause()方法中取消註冊。但是,它似乎被阻止,雖然我註冊在OnResume(),它不會觸發。如果我只註冊它,它工作得很好,但後來我得到的異常「跑冒滴漏廣播接收器,你應該取消註冊」,如下圖所示:無法在Android中取消註冊Broadcast Receiver?

06-04 22:50:04.267: ERROR/ActivityThread(4188): Activity com.example.android.CView has leaked IntentReceiver [email protected] 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 [email protected] 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()取消註冊。我試過了,然後它從不聽。

感謝您的指點!

回答

3

實踐是在OnResume中註冊它並在OnPause中註銷它,對不對?

是的。 Here is a sample application證明了這一點。

+0

我試過你的示例鏈接,並以相同的方式完成,但仍然即時獲取錯誤在onPause錯誤鏈接http://pastie.org/9825190我的代碼是http://pastie.org/9825223 – user3233280 2015-01-11 10:42:39

+0

@ user3233280:我不知道爲什麼你會得到這個錯誤。 – CommonsWare 2015-01-11 12:07:48

+0

一旦ACTION_POWER_CONNECTED廣播reciver調用,用於從Service更新UI的任何優秀教程? – user3233280 2015-01-11 12:15:52

相關問題