41720

Code for checking Internet connection is not working properly

Question:

I am developing an app in which I need to check if an internet connection is available or not. The code I have written seems to handle all cases but it is not working in my emulator. My code:

public boolean hasConnection() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetwork = cm .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetwork != null && wifiNetwork.isConnected()) { return true; } NetworkInfo mobileNetwork = cm .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobileNetwork != null && mobileNetwork.isConnected()) { return true; } NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null && activeNetwork.isConnected()) { return true; } return false; }

The problem is that it is never returning false whether the internet connection is available or not, both in a device and in my emulator. But it is working well if the device is connected with wifi connection. What problem with that snippet?

Answer1:

Try this method.

private boolean IsNetworkAvailable() { ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mActiveNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); return mActiveNetworkInfo != null && mActiveNetworkInfo.isConnected(); }

You will also need:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Recommend

  • Code for checking Internet connection is not working properly
  • Android: How can we know the current connection type (ex. Wifi, 3G, 2G…) programmatically?
  • Broadcast receiver is not calling on internet connectivity check
  • How to check whether a particular device supports 4G networks in Android?
  • Parcelable and JSON in android
  • Android: Uploading images to server using fragments and navigation drawer
  • BroadcastReceiver is not working (detect if wifi is connected)
  • How to enable the background data settings in android?
  • Notification every time wifi is disconnected [duplicate]
  • How to receive missed messages on resuming connection lost in pubnub?
  • How to use broadcast and receivers
  • Facebook FQL: I want to get all the posts that a user wrote on friends timelines
  • Matplotlib change colormap tab20 to have three colors
  • Replace repeating delimiters in a text file with an alternate character
  • How to set the height of a PDF header?
  • How to mention namespace in XSLT file when the namespaces are dynamic?
  • Every second request using Apache HTTPClient fails
  • Getting SQLException: Driver:org.hsqldb.jdbcDriver returned null for URL in tomee
  • How can I get the concrete type of a generic type variable using mirrors in Dart?
  • Android sensor listener does not get unregistered or sensor still retrieves values
  • Qpython unicode strings
  • Push notifications don't work
  • AlarmManager is blocking main thread
  • Why is my AlarmManager firing off instantly?
  • onLeScan never gets called android
  • Missing permission to control media error on Android 5/L
  • MVC - @Html.CheckBoxFor
  • Spring: No transaction manager has been configured
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • System.InvalidCastException: Specified cast is not valid
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • PHP - How to update data to MySQL when click a radio button
  • Javascript Callbacks with Object constructor
  • Bitwise OR returns boolean when one of operands is nil