Has leaked IntentReceiver... that was originally registerd here. Are you missing a call to unregister, intentreceiver

Source: Internet
Author: User

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.