Android code snippet to check if Internet connectivity is available on Android devices and is active. Check connections like Wifi and Mobile Subscriber Internet connections like 2g, 3g, 4g, etc.
ConnectivityManager is the class you can make use of to achieve this, use getSystemService(Context.CONNECTIVITY_SERVICE) that returns an ConnectivityManager object, now using this connection manager you can get information of all networks as an NetworkInfo[] array. Iterate through this array and check what type of network is currently active with the device - WIFI or MOBILE using netInfo.getTypeName().equalsIgnoreCase(TYPE).
private boolean checkNetwork() {
boolean wifiAvailable = false;
boolean mobileAvailable = false;
ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conManager.getAllNetworkInfo();
for (NetworkInfo netInfo : networkInfo) {
if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
if (netInfo.isConnected())
wifiAvailable = true;
if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
if (netInfo.isConnected())
mobileAvailable = true;
}
return wifiAvailable || mobileAvailable;
}
If you see any error in the logCat, it could be that you have missed adding the required permissions!
⚠️ Add the following User Permission: android.permission.ACCESS_NETWORK_STATE
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
- Fix: No Internet Connection on iPhone WiFi Network - iOS
- Eclipse : This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Eclipse
- How to Show Full Website Address on Safari Browser - MacOS
- How to Set JAVA_HOME PATH to Mac .zshrc profile file - Java
- Chrome : When Adobe flash player has finished updating, reload this page to active it - Chrome
- How to see Mac Login and Shutdown history for audit - MacOS
- Configure AWS Access ID and Secret Keys using CLI on Mac - AWS
- Sublime Text 3 spell check shortcut - Sublime