Search code examples
androidbroadcastonresume

Why receiver receive broadcast every time the onResume() method activated?


In my onresume() of activity, I have this

IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mReceiver, intentFilter);

Every time the onResume() activate, mReceiver always received a broadcast have the action of ConnectivityManager.CONNECTIVITY_ACTION

That's strange, because my networkstate is in a stable wifi and does not changed at all.


Solution

  • Seems sticky broadcasts is a common problem for action ConnectivityManager.CONNECTIVITY_ACTION, Follow the instruction I tried to describe the receiver in XML but it seems NOT OK with that AT last,I used a flexible method to resolve this:Since everytime in Onresume will send that broadcast,I set a boolean value to be 'false',when the onresume called the receiver for the first time,the program will do nothing but just set this boolean value to 'true'. Then when CONNECTIVITY changed,it will go normal process. This seems not an elegant method ,but it worked in my app.