¿Cómo comprobar la conexión de red habilitar o deshabilitar en WIFI y 3G (plan de datos) en el móvil?

Estoy desarrollando una aplicación Android, en mi aplicación, quiero comprobar la conexión de red, como quiero comprobar la conexión de red en wifi y 3G (como los indios en su mayoría como plan de datos en el móvil), cómo comprobar la red en wifi y 3G.naybody Sabe, por favor dar alguna idea acerca de eso.

Gracias

Aquí está el fragmento de código. Devuelve true si se habilita la red, sino false

private boolean netCheckin() { try { ConnectivityManager nInfo = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); nInfo.getActiveNetworkInfo().isConnectedOrConnecting(); Log.d(tag, "Net avail:" + nInfo.getActiveNetworkInfo().isConnectedOrConnecting()); ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { Log.d(tag, "Network available:true"); return true; } else { Log.d(tag, "Network available:false"); return false; } } catch (Exception e) { return false; } } 

Por favor, intente esto

 public static boolean isInternetConnected (Context ctx) { ConnectivityManager connectivityMgr = (ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); // Check if wifi or mobile network is available or not. If any of them is // available or connected then it will return true, otherwise false; if (wifi != null) { if (wifi.isConnected()) { return true; } } if (mobile != null) { if (mobile.isConnected()) { return true; } } return false; } 

Añada el permiso de abajo en el archivo de manifiesto de android.

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

Puede utilizar el código siguiente esto funciona para todas las versiones de API:

 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); if(isConnected) { if(activeNetwork.getType()==ConnectivityManager.TYPE_MOBILE) return true; else return false; } else return false; 

Estoy usando esto dentro de la clase de actividad

 private boolean isConnectedToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); return (networkInfo != null); } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.