Last active
August 29, 2015 14:00
-
-
Save enginebai/394b8291000e9f793e5a to your computer and use it in GitHub Desktop.
The code can detect the network status on Android mobile.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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