Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ParryPatel021/2a078cf57d4e15ad71114629e6efed47 to your computer and use it in GitHub Desktop.
Save ParryPatel021/2a078cf57d4e15ad71114629e6efed47 to your computer and use it in GitHub Desktop.
Check internet connection in android.
//This method will return true if internet available else return false.
public static boolean checkInternetConnection(Context mContext) {
ConnectivityManager connection = (ConnectivityManager) mContext.getSystemService(CONNECTIVITY_SERVICE);
if (connection != null) {
if (connection.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connection.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
connection.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ||
connection.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
return true;
} else if (connection.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||
connection.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
return false;
} else {
return false;
}
}
return false;
}
<!-- permission require to check the connectivity -->
<!-- Add below lines in AndroidManifest.xml file -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment