Android Broadcast dynamic registration Duplicate registration problem

Source: Internet
Author: User

Recently in the study of the content of the Android broadcast, according to the example of the book to write a small program to achieve mandatory offline function, the general operating process is:
Login input account password into the main interface, click on the Main Screen button pop-up dialog box prompts are forced offline. Click OK to display the login page.
The results of the first kick is good, and then log in after the kick will appear two times the prompt dialog box. The third time you are kicked, you will be three times ... I use dynamic registration in the program, so I change to static registration is no problem. Think about what seems to be the problem here, it should be repeated registration of the broadcast received. After the problem is found, the broadcast receive object is defined as a static object, initialized only once, and the problem is solved. That is, with dynamic registration, a broadcast-received object is generated each time it is registered.

Here's the code for the problem:

public class Mainactivity extends baseactivity {Button sendbtn;
    Localbroadcastmanager Localbroadcastmanager;
    Intentfilter Intentfilter;

    Sets the broadcast receiver object to static Bootbroadcasereceiver Mybroadcastreceiver;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_main);
        Localbroadcastmanager = Localbroadcastmanager.getinstance (this);
        SENDBTN = (Button) Findviewbyid (R.id.sendbutton);
                Sendbtn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
                Intent Intent = new Intent ("Com.ilrose.www.MYBROADCAST");
                Localbroadcastmanager.sendbroadcast (Intent);
LOG.I ("Turisla", "send");
            Sendbroadcast (Intent);
        }
        });
        When initialized, the decision is null, and it is null to initialize it, thus ensuring that there is always one. if (Mybroadcastreceiver = = null) {LOG.I ("tUrisla "," register ");
            Intentfilter = new Intentfilter ();
            Intentfilter.addaction ("Com.ilrose.www.MYBROADCAST");
            Mybroadcastreceiver = new Bootbroadcasereceiver ();
        Localbroadcastmanager.registerreceiver (Mybroadcastreceiver, Intentfilter); }
    }
}


Related Article

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.