It is always preferred to check Internet Connection before fetching/posting data over http while working with Android Application.
ConnectivityManager class is used to check network connectivity. This class also notifies applications when network connectivity changes say from 2G to 4G or WiFi.
ConnectivityManager class responsibilities are :
- To Monitor network connections (Wi-Fi, GPRS, 2G,3G, etc.)
- Send Broadcast intents when network connectivity changes (say Mobile to Wifi)
- Attempt to fail over to another network when connectivity to a network is lost
- Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks.
Below method check if any of the network (Data Plan ie. Mobile or Wifi ) is active on the Android Device and returns a boolean value.
//Check if Internet Network is active
private boolean checkNetwork() {
boolean wifiDataAvailable = false;
boolean mobileDataAvailable = false;
ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conManager.getAllNetworkInfo();
for (NetworkInfo netInfo : networkInfo) {
if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
if (netInfo.isConnected())
wifiDataAvailable = true;
if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
if (netInfo.isConnected())
mobileDataAvailable = true;
}
return wifiDataAvailable || mobileDataAvailable;
}
More Posts related to Android,
- Share or Send SMS via Android Intent
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Fixed Length Android Edittext
- Error:The SDK Build Tools revision (XX.X.X) is too low for project. Minimum required is XX.X.X
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Pass data between two Android Activities and access it using Intent
- AlertDialog with no buttons (just text message)
- Android Eclipse This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in
- Can't Run SDK Manager find_java.bat issue
- Android Studio Native typeface cannot be made error
More Posts:
- How to create Custom RatingBar Android Programming Tutorial - Android
- How to use HashTags # correctly on Social Media Facebook Twitter - HowTos
- connection.url property value in hibernate.cfg.xml for mysql - Java
- Android AlertDialog with 3 buttons example - Android
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete - Android
- remove div vertical scroll - Html
- Android Display Toast on Button Click - Android
- Check Internet Connection WIFI 4G is active on Android Programmatically - Android
- Device not compatible error Android Google Play Store - Android
- [Mac] To open Eclipse you need to install the legacy java se 6 runtime - Mac-OS-X
- 86 Gmail keyboard shortcuts that you may find Advantageous - Google
- Maven Eclipse (M2e) No archetypes currently available - Java
- Tutorial: CSS Media Query for Responsive Web Design - CSS
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps - HowTos
- ActivityManager Warning: Activity not started, its current task has been brought to the front - Android