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
- Copy Mac Screenshot in Clipboard instead of a png file - MacOS
- Only Chessboard using HTML and CSS Code Example - CSS
- How to Update Safari on Mac to the Latest Version [macOS] - MacOS
- How to Download Apple Vision Pro visionOS Simulator on Xcode 15 - Apple
- How to Provide Full Disk Access to App on Mac - MacOS
- Find Mac version using terminal command - MacOS
- Install Eclipse IDE on M1/M2 Mac Natively - Eclipse
- Fix - Unsupported major.minor version 65.0 (Java JDK 21) - Java-JDK-21