views:

539

answers:

3

Does Android broadcast an intent when the network state has changend, i.e. from GSM to Wifi? And if so what permission do I need and what is the intent action called?

+1  A: 

Android will broadcast WifiManager.NETWORK_STATE_CHANGED_ACTION when the devices gains or looses wifi connectivity. You'll need the permission android.permission.ACCESS_WIFI_STATE to receive the intent. However, I'm not sure if there's something similar for GSM -> WIFI.

Erich Douglass
+3  A: 

It broadcasts whenever you connect or disconnect from Wifi, in other words, Wifi State.

You can do it using the following intent-filters:
- android.net.wifi.WIFI_STATE_CHANGED
- action android:name="android.net.wifi.STATE_CHANGE
- android.net.wifi.supplicant.CONNECTION_CHANGE

Which needs the following permission:
- uses-permission android:name="android.permission.ACCESS_WIFI_STATE"

And on your BroadcastReceiver you can see if the phone has connected to an actual Wi-fi spot the following way:

final String action = intent.getAction();

if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
{
  NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  if (info.getState().equals(NetworkInfo.State.CONNECTED))
  {
    //do whatever you want when wifi is active and connected to a hotspot
  }
}

You can also use:
if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))

and/or

if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))

to see if the user turned off wi-fi or the connection dropped, respectively.

velazcod
+3  A: 

There isn't anything exactly like what you are describing but you can use ConnectivityManager.CONNECTIVITY_ACTION to monitor changes and use the extras that come with it to see if the connection has changed from GSM to Wifi and vice versa. For this you need the permission android.permission.ACCESS_NETWORK_STATE and android.permission.ACCESS_WIFI_STATE for checking wifi information.

Al