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
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
- How to Quit Applications on Mac Using Terminal - MacOS
- Quick way to setup AWS DynamoDB locally on macOS - AWS
- FCM Messages Test Notification!!!! - Microsoft Teams, Google Hangouts push alert - News
- Hide Navigation Bar from Android Screen Activity - Android
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint
- How to install Python on Ubuntu - Ubuntu
- Notepad++ Export file as HTML - NotepadPlusPlus
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew - Python