How to detect in Android if the device changed the type of Wi-Fi network to data and know the name of the network? is that I need to get this information from the network to validate and execute a method.
How to detect in Android if the device changed the type of Wi-Fi network to data and know the name of the network? is that I need to get this information from the network to validate and execute a method.
To detect changes in the network you must use a BroadcastReceiver
This is an example class to detect changes
public class NetworkStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if(intent.getExtras() != null) {
NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
Log.i("Network", "Network connected : " + ni.getTypeName());
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
Log.i("Network", "No existe conectividad!");
}
}
}
You have to register on your AndroidManifest.xml the BroadcastReceiver , with the intent-filter% % co:
<receiver android:name=".NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and add permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />