Question

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.

Was it helpful?

Solution 3

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.

OTHER TIPS

every time onresume is activited , registerReceiver is called one more each time. registerReceiver sends bck sticky broadcasts. Sticky broadcasts are sent to receiver as soon as registerBroadcast is called.

you can use unregisterReceiver or check you will have to check whether you have already registered and skip , if yes..

As per documentation for registerReceiver (BroadcastReceiver receiver, IntentFilter filter):

The system may broadcast Intents that are "sticky" -- these stay around after the broadcast as finished, to be sent to any later registrations. If your IntentFilter matches one of these sticky Intents, that Intent will be returned by this function and sent to your receiver as if it had just been broadcast.

Though you are just registering your receiver right now but it's up to the system when to broadcast that Intent and since that intent is a sticky one so it gets broadcasted as soon as you register.

Check out this ConnectivityManager.CONNECTIVITY_ACTION, always broadcast when registering a receiver?

A solution is to use isInitialStickyBroadcast in the onReceive callback of your BroadcastReceiver to know if you are actually proceeding a sticky broadcast and act accordingly (BroadcastReceiver : isInitialStickyBroadcast)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top