I'm developing a mobile app that would need to recover its connection when the mobile phone is switching from wifi to mobile, from mobile to wifi, from wifi or mobile to no network and from no network to wifi or mobile.
I already wrote a BroadcastReceiver object which catches the CONNECTIVITY_CHANGE intents and I got a ConnectivityManager object from the context.Then I create a NetworkInfo object from this.
The question is: why do I get two intents each time I switch the network type (I'm getting only one message when I switch off the network).
I guess that the first intent's purpose is to notify that the active connection is off and the second intent is to notify that the system switched to the other active connection but I'm not sure about that and how to test it in the receiver.
Does anyone have a code snippet to handle properly this kind of event?
Thanks for your answer.
Regards
This is the answer to your question:
Why do I get two intents each time I switch the network type (I'm getting only one message when I switch off the network).
You get two calls because "CONNECTIVITY_CHANGE" sends a broadcast when a connection has either been established or lost as per googles documentation, so, when you change network type, you lost connectivity in one(that's one call) and you established connectivity in another network(second call), now when you switch off the network you only get the connectivity lost call. Notice that "CONNECTIVITY_CHANGE" only let you know about actions going on with the networks but you have to query that information from ConnectivityManager when that happens to know what network has lost / establish connection etc...
Hope it Helps.
Regards!