1

I am checking the network status of a device. using:-

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();

I want to take this on a little further and check to see if the device can access the internet or a given site such as http://www.google.com and if it can then return as true and if it can't then return as false.

The problem with my code is that it will return true if the device is connected to a router and the router is offline. I need an actual internet connection check.

Amended code

public void GoToStation(View v)
{

    try {
        InetAddress ina = InetAddress.getByName("http://188.65.176.98/");

        if(ina.isReachable(3000)) {

                  Intent myIntent = new Intent(MainActivity.this, CustomizedListViewStation.class);
                  startActivityForResult(myIntent, 0);

        } else {
            Toast.makeText(this, "You need a data connection to view Safety Zones", Toast.LENGTH_LONG).show(); 
        } 
    } catch (IOException ioe) {
        ioe.printStackTrace();

    }

2 Answers2

4

You could do something like this:

boolean isAvailable(URL url){
    try {
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.connect();
      urlConnection.disconnect();
      return true;
    } catch (IOException e) {
          Log.e(TAG, "Exception", e);
    }
    return false;
}
Asahi
  • 13,378
  • 12
  • 67
  • 87
  • Opening a full HTTP connection seems a little heavy just to check if the user has an active internet connection. – shanet Aug 07 '13 at 20:16
  • I guess he wants to check if the URL is working, not whether there is an internet connection. So the answer suggestion is valid. – Mahmut K. Mar 16 '23 at 20:52
2

To check for an actual internet connection, you could try to access some remote server. For example:

try {
    InetAddress ina = InetAddress.getByName([server]);

    if(ina.isReachable(3000)) {
        System.out.println("Internet Connection");
    } else {
        System.out.println("No internet connection");
    } 
} catch (IOException ioe) {
    ioe.printStackTrace();

}

Where [server] is the server to try to connect to. You should use your own server for this since it's rude to use someone else's bandwidth for this purpose (plus using your own server is more reliable).

Another route is to check if there's an Android API that does this since Android checks for a connection to Google's servers. I'm not sure if this status is exposed in the API anywhere though.

shanet
  • 7,246
  • 3
  • 34
  • 46
  • shanet Thanks for the reply. How could I add your example to the code I already have so that it returns true for a connection and false for no connection on return activeNetworkInfo != null && activeNetworkInfo.isConnected(); – Robbie Birkett Aug 07 '13 at 18:53
  • @RobbieBirkett Drop it in a function and replace the println lines with `return true/false`. I should also add that this is (obviously) doing network IO so it should be put on a non-UI thread or Android will complain. – shanet Aug 07 '13 at 19:00
  • what would I put where [server] is? A URL such as my homepage? – Robbie Birkett Aug 07 '13 at 19:25
  • sorry I see this needs to be the IP address. I have added my amended code to the main thread can you see why it would not work? – Robbie Birkett Aug 07 '13 at 19:30