Has leaked IntentReceiver... that was originally registerd here. Are you missing a call to unregister, intentreceiver
Today I encountered this error: has leaked IntentReceiver... that was originally registerd here. Are you missing a call to unregisterReceiver
:
The Chinese meaning of this error is that IntentReceiver has exceeded. Have you forgotten to call the method for canceling broadcast?
Cause:
The reason for IntentReceiver overflow is that the receiver is not unregister after it is register. after repeated attempts, intentReceiver leaks.
However, I actually called the unregisterReceiver (batteryReceiver). Why does this problem occur?
Solution:
To avoid this problem, register the broadcast and cancel the broadcast in pairs, but at the same time to avoid the problem of NULL pointer, we 'd better add a flag
/*** Flag, whether to enable broadcaseReceiver. The default value is false */boolean flag = false;
When registering a broadcast, change the flag to true.
/*** Register broadcast */private void registerReceiver () {flag = true; IntentFilter filter = new IntentFilter (); filter. addAction (Intent. ACTION_BATTERY_CHANGED); batteryReceiver = new BatteryReceiver (); registerReceiver (batteryReceiver, filter );}
Before canceling a broadcast, determine whether the flag is true (that is, whether the broadcast has been registered). If so, cancel the broadcast.
@Overridepublic void onPause() {if(flag){flag=false;unregisterReceiver(batteryReceiver);}super.onPause();}
The reason for flag is to avoid registration of broadcast, but cancel broadcast, so there will be a null pointer
Reprinted please indicate the source, thank you http://blog.csdn.net/harryweasley/article/details/45092041