Has leaked intentreceiver ... that is originally registerd here. Is you missing a call to unregister

Source: Internet
Author: User




Today I encountered this error: has leaked intentreceiver ... that is originally registerd here. Is you missing a call to Unregisterreceiver




The Chinese meaning of this error is that it has overflowed the intentreceiver, did you forget to call the method of canceling the broadcast?


Reason for the problem:

The reason is overflow intentreceiver, because there is receiver has not been unregister after the register, repeated several times after causing intentreceiver leakage


But I actually have to call Unregisterreceiver (Batteryreceiver), then why this problem still occurs


Workaround:

In order to avoid this problem, register the broadcast and write off the broadcast to appear in pairs , but at the same time in order to avoid the problem of NULL pointers, we'd better add a flag-bit flag


/** * Flag bit, whether Broadcasereceiver open, default to False */boolean Flag=false;

Change flag to True when registering a broadcast

/** * Register broadcast */private void Registerreceiver () {flag=true;intentfilter filter = new Intentfilter (); Filter.addaction (Intent . action_battery_changed); batteryreceiver=new batteryreceiver (); Registerreceiver (batteryreceiver, filter);}


Before cancelling the broadcast, first determine if flag is true (that is, whether the broadcast has been registered), or if true, to unregister the broadcast
@Overridepublic void OnPause () {if (flag) {flag=false;unregisterreceiver (batteryreceiver);} Super.onpause ();}



The reason to add flag is to avoid not registering the broadcast, but to cancel the broadcast, so there will be a null pointer


Reprint please indicate the source, thank http://blog.csdn.net/harryweasley/article/details/45073065




Has leaked intentreceiver ... that is originally registerd here. Is you missing a call to unregister

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.