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); }
}
}