Skip to content

Instantly share code, notes, and snippets.

@enginebai
Last active August 29, 2015 14:00
Show Gist options
  • Save enginebai/394b8291000e9f793e5a to your computer and use it in GitHub Desktop.
Save enginebai/394b8291000e9f793e5a to your computer and use it in GitHub Desktop.
The code can detect the network status on Android mobile.
public void checkNetworkType()
{
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo networkActive = connectivityManager.getActiveNetworkInfo();
NetworkInfo networkWifi = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI );
boolean connect = false;
if ( connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI ).getState() == NetworkInfo.State.CONNECTED )
connect = true;
String networkSummary = String.format( "Connect? %s, Active=%s\n"
+ "WiFi Status=%s\nWiFi Available? %s\nWiFi Connect? %s\n",
connect ? "O" : "X",
networkActive == null ? "Null" : networkActive.getTypeName(),
networkWifi.getState(), networkWifi.isAvailable() ? "Yes" : "No", networkWifi.isConnectedOrConnecting() ? "Yes" : "No" );
AlertDialog.Builder summaryDialog = new AlertDialog.Builder( this );
summaryDialog.setTitle( this.getResources().getString( R.string.app_name ) );
summaryDialog.setMessage( networkSummary );
summaryDialog.setPositiveButton( "Ok", null );
summaryDialog.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment