How to detect in Android if the device changed the type of wifi network to data and know the name of the network?

4

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.

    
asked by JDeveloper 23.06.2016 в 03:43
source

1 answer

3

To detect changes in the network you must use a BroadcastReceiver

link

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" />
    
answered by 23.06.2016 в 06:30