Code2care : TechZone Apps & Tutorials

How to check if Internet connection is available and active on Android Device(Wifi or Mobile)

Android code snippet to check if Internet connectivity is available on Android device and is active. Check connections like Wifi and Mobile Subscriber Internet connections like 2g, 3g, 4g etc.

Add the following User Permission : android.permission.ACCESS_NETWORK_STATE

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;
	}






Popular tags
android
x 175
eclipse
x 29
notepadplusplus
x 20
macosx
x 14
sharepoint
x 14
html
x 14
mac
x 13
androidstudio
x 11




1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


Code2care © 2012-17