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 required permissions!
⚠️ Add the following User Permission: android.permission.ACCESS_NETWORK_STATE
- Android appcompat_v7 Error retrieving parent for item: No resource found that matches the given name
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- Center align text in TextView Android Programming
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Android : Remove ListView Separator/divider programmatically or using xml property
- Copy Text to Android Clipboard Programmatically ClipboardManager
- How to add Newline to text in Android TextView
- How to Change Android Toast Position?
- Android AlertDialog with 3 buttons example
- [Solved] Notepad++ Menu Bar Missing - NotepadPlusPlus
- Copy all .vcf Mobile Contacts files into one .vcf - HowTos
- Add blank lines after each lines using Notepad++ text editor - NotepadPlusPlus
- How to set background color for android layout pragmatically using java and through xml - Android
- Share or Send SMS via Android Intent - Android
- Android Emulator window was out of view and was recentered - Android-Studio
- Multiline EditText in Android Example - Android
- Right Align Text in Bootstrap framework - Bootstrap
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml - SharePoint
- Division between two numbers - C-Program
- How to send email from JavaScript HTML using mailto - JavaScript
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio - Android-Studio
- How to remove disable google search blue triangle - Google
- How to update Android Studio - Android-Studio
- Add Buttons at the bottom of Android Layout xml file - Android