2013-12-22 84 views
0

我在我的onCreate()下面的代碼:泄露IntentReceiver

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

此行似乎是拋出以下錯誤:Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?

我只是不明白這是如何可能的 - 但我完全卸載我的應用程序,然後再次安裝它,這是整個應用程序中唯一的接收器註冊。有誰知道什麼是錯的?

+1

你註銷接收器呢? – JoxTraex

回答

1

您需要註銷您的接收器中的onPause:

@Override 
protected void onPause() { 
    // Unregister since the activity is not visible 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(onComplete); 
    super.onPause(); 
} 
+0

兩點:OP似乎並沒有使用LocalBroadcastManager,所以應該使用'Activity.unregisterReceiver(..)';其次(對於OP),在Activity生命週期(onCreate/onDestroy; onStart/onStop;或onResume/onPause)中匹配對方法中註冊/註銷接收者是一種好的做法。 –

+0

我正在這樣做,但似乎沒有解決問題。 –