Effective way to check for slow internet connection and differentiate between network connection and


How to check if the device is connected to internet or it is just connected to external wifi network? Because NetworkInfo returns true if a device is connected to an external wifi network even there is no network connection.

When device is connected to wifi but there is no internet access,my app's network connectivity-check class returns true but application crashes with the exception that it cannot access the corresponding http url.


If you want to know when you have an active internet connection, do something like this:

Some static variables:

/** * Set the number of retries when reestablishing Internet connection. */ private static int retryConnectionNumber = 0; /** * The maximum number of retries allowed for Internet connection. */ private final static int CONNECTION_RETRY_MAX = 5; /** * The timeout of the HTTP request when checking for Internet connection. */ private final static int REQUEST_TIMEOUT = 2000;

The method to check if network is available:

private static void isNetworkAvailable(final Handler handler, final int timeout) { new Thread() { private boolean responded = false; @Override public void run() { URL url = null; try { url = new URL("http://your_server_addres.com"); } catch (MalformedURLException e1) { e1.printStackTrace(); } String host = ""; if (null != url) { host = url.getHost(); } Log.i("NetworkCheck", "[PING] host: " + host); Process process = null; try { process = new ProcessBuilder() .command("/system/bin/ping", "-c 1", "-w " + (timeout / 1000), "-n", host) .redirectErrorStream(true).start(); } catch (IOException e) { e.printStackTrace(); } InputStream in = process.getInputStream(); StringBuilder s = new StringBuilder(); int i; try { while ((i = in.read()) != -1) { s.append((char) i); if ((char) i == '\n') { Log.i("NetworkCheck", "[PING] log: " + s.toString()); if (s.toString().contains("64 bytes from")) { // If there were a response from the server at // all, we have Internet access responded = true; } s.delete(0, s.length()); } } } catch (IOException e) { e.printStackTrace(); } finally { // Destroy the PING process process.destroy(); try { // Close the input stream - avoid memory leak in.close(); } catch (IOException e) { e.printStackTrace(); } // Send the response to the handler, 1 for success, 0 // otherwise handler.sendEmptyMessage(!responded ? 0 : 1); } } }.start(); }

The handler:

/** * Handler used that receives the connection status for the Internet. * If no active Internet connection will retry #CONNECTION_RETRY_MAX times */ private static Handler listenForNetworkAvailability = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what != 1) { // code if not connected Log.i("NetworkCheck", "not connected"); if (retryConnectionNumber <= CONNECTION_RETRY_MAX) { Log.i("NetworkCheck", "checking for connectivity"); Here you could disable & re-enable your WIFI/3G connection before retry // Start the ping process again with delay Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { isNetworkAvailable(listenForNetworkAvailability, REQUEST_TIMEOUT); } }, 5000); retryConnectionNumber++; } else { Log.i("NetworkCheck", "failed to establish an connection"); // code if not connected } } else { Log.i("NetworkCheck", "connected"); retryConnectionNumber = 0; // code if connected } } }


Something like this would work:

private boolean haveNetworkConnection() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; }

I have copied this code from <a href="https://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android" rel="nofollow">here</a> (I understand the code - I am just adding the code here for ease for the user + I got told by an admin that you should do this and credit the original post). Personally I would split this into two functions, one for mobile and one for wifi, but that choice is up to you.


  • Why does it draw lines in the wrong place?
  • pyodbc doesn't report sql server error
  • NSScanner Loop Question
  • Display java JPanel in a JFrame
  • GridView breaks while scrolling
  • Converting a WriteableBitmap image ToArray in UWP
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • how to adjust image in a panel in Java swing?
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Why value captured by reference in lambda is broken? [duplicate]
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • javascript inside java/jsp code
  • Perl system calls when running as another user using sudo
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Circular dependency while pushing http interceptor
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • LevelDB C iterator
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?