Created
July 20, 2018 13:40
-
-
Save ParryPatel021/2a078cf57d4e15ad71114629e6efed47 to your computer and use it in GitHub Desktop.
Check internet connection in android.
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
//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; | |
} |
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
<!-- 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