18

According to the Android developer site, Determining and Monitoring the Connectivity Status, we can check there is an active Internet connection. But this is not working if even only Wi-Fi is connected and not Internet available (it notifies there is an Internet connection).

Now I ping a website and check whether Internet connections are available or not. And this method needs some more processing time. Is there a better method for checking Internet connectivity than this to avoid the time delay in ping the address?

10
  • Sorry but it is working with Wifi too. Many developers are using this but still no issue. Even I am using the same function since many years and I am not facing any issue with WIFI or Mobile Data connection for internet connectivity. Commented Jul 5, 2017 at 6:32
  • 1
    Yes I am also using same method i.e ping google to check internet connectivity and yes it takes some time, but I think its the only solution we have got at the moment. Commented Jul 5, 2017 at 6:38
  • @AbdulKawee I am fully agree with you. Commented Jul 5, 2017 at 6:43
  • 1
    But on the device, we can see (in wifi settings) there is no internet text if there is no internet connection in wifi. Is there any possibility to get that data.Currently, i am checking the GitHub code for wifi settings :) Commented Jul 5, 2017 at 6:50
  • 2
    FYI simply pinging Google/Google DNS can result in false positive if the phone is connected to office/hotel/cafe guest wifi where all request are redirected to portal login. A more robust way is to use Microsoft's NCSI where you can tell if there's actual internet connection as opposed to portal login page. So either do a dns lookup for dns.msftncsi.com, verify it's 131.107.255.255 or download the www.msftncsi.com and verify you get a text file containing "Microsoft NCSI"
    – Martheen
    Commented Jul 5, 2017 at 7:57

10 Answers 10

10

Try this:

It's really simple and fast:

public boolean isInternetAvailable(String address, int port, int timeoutMs) {
    try {
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress(address, port);

        sock.connect(sockaddr, timeoutMs); // This will block no more than timeoutMs
        sock.close();

        return true;

    } catch (IOException e) { return false; }
}

Then wherever you want to check just use this:

if (isInternetAvailable("8.8.8.8", 53, 1000)) {
     // Internet available, do something
} else {
     // Internet not available
}
7
  • 3
    Why would you check the availability of Google DNS rather than the actual site you want to interact with? Also, the OP claim they already do this, and are looking for a better method. Commented Jul 5, 2017 at 9:18
  • 1
    that's because this way it'll be fast. the actual site might be slow due to heavy traffic sometimes.
    – sumit
    Commented Jul 5, 2017 at 9:21
  • 3
    If the actual site is slow or unresponsive, the code interacting with it will be slow or unresponsive as well, even if the connectivity check is fast. Commented Jul 5, 2017 at 9:26
  • 2
    nop the problem of the user was just to check whether Internet is available or not and that too quickly. As he has mentioned in the question he was also pinging the actual site but that method was slow. I provided the solution just to check whether internet is available or not.
    – sumit
    Commented Jul 5, 2017 at 9:30
  • 2
    You forgot to close your socket in an error condition, if you code like that, you will get risks of running out of file descriptors
    – Ferrybig
    Commented Jul 5, 2017 at 10:29
4

The first problem you should make it clear is what do you mean by whether internet is available?

  • Not connected to wifi or cellular network;
  • Connected to a limited wifi: e.g. In a school network, if you connect to school wifi, you can access intranet directly. But you have to log in with school account to access extranet. In this case, if you ping extranet website, you may receive response because some intranet made auto redirect to login page;
  • Connected to unlimited wifi: you are free to access most websites;

The second problem is what do you want to achieve?

As far as I understand your description, you seems want to test the connection of network and remind user if it fails. So I recommend you just ping your server, which is always fine if you want to exchange data with it.


You wonder whether there is a better way to test connectivity, and the answer is no.

The current TCP/IP network is virtual circuit, packet-switched network, which means there is no a fixed 'path' for the data to run, i.e. not like a telephone, we have a real connection between two users, we can know the connection is lost immediately after circuit is broken. We have to send a packet to the destination, and find no response, then we know, we lose the connection (which is what ping -- ICMP protocol -- does).

In conclusion, we have no better way to test the connectivity to a host other than ping it, that is why heartbeat is used in service management.

3
  • @ Tony , I think the description is enough to understand the problem I am facing now Commented Jul 5, 2017 at 11:52
  • As far as I understand your updated version, you want to test connectivity to a specific site, so there is no better way if you want to exchange data with it.
    – Tony
    Commented Jul 5, 2017 at 13:01
  • Yes i found that and also checked the google code for Android wifi settings. Commented Jul 5, 2017 at 13:35
4

Try the following:

public boolean checkOnlineState() {
    ConnectivityManager CManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo NInfo = CManager.getActiveNetworkInfo();
    if (NInfo != null && NInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

Don't forget the access:

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

Else:

if (InetAddress.getByName("www.google.com").isReachable(timeout))
{    }
else
{    }
2

On checking this issue it found that We cannot determine whether an active internet connection is there, by using the method specified in the developer site: https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

This will only check whther ther active connection of wifi.

So I found 2 methods which will check whether there is an active internet connection

1.Ping a website using below method

URL url = new URL(myUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        // 30 second time out.
        httpURLConnection.setConnectTimeout(30000);
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            isAvailable = true;
        }

2.Check the availability of Google DNS using socket

  try {
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

        sock.connect(sockaddr, 1000); // this will block no more than timeoutMs
        sock.close();

        return true;
} 

The second method is little faster than 2nd method (Which suits for my requirement)

Thanks all for the answers and support.

2

I wanted to comment, but not enough reputation :/

Anyways, an issue with the accepted answer is it doesn't catch a SocketTimeoutException, which I've seen in the wild (Android) that causes crashes.

public boolean isInternetAvailable(String address, int port, int timeoutMs) {
    try {
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress(address, port);

        sock.connect(sockaddr, timeoutMs); // This will block no more than timeoutMs
        sock.close();

        return true;

    } catch (IOException e) { 
        return false; 
    } catch (SocketTimeoutException e) {
        return false;
    }
}
0
2

//***To verify internet access

public static Boolean isOnline(){

    boolean isAvailable = false;
    try {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        URL url = new URL("https://stackoverflow.com/");
        HttpURLConnection httpURLConnection = null;
        httpURLConnection = (HttpURLConnection) url.openConnection();

        // 2 second time out.
        httpURLConnection.setConnectTimeout(2000);
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            isAvailable = true;
        } else {
            isAvailable = false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        isAvailable = false;
    }

    if (isAvailable){
        return true;
    }else {
        return false;
    }
}
3
  • 1
    Please add some explanation / inline code comments to explain how your code works and will solve OP's query. Commented Nov 1, 2018 at 13:19
  • if(isOnline()){ your code } else{TOAST: ERROR_NOT_CONNECTION_TRY_AGAIN} Commented Nov 3, 2018 at 6:10
  • it returns false too late so you can not be able to toast when you have wifi signals but not internet connection.
    – Nafees
    Commented Nov 12, 2018 at 9:45
1

ConnectivityManager will not be able to tell you if you have active connection on WIFI.

The only option to check if we have active Internet connection is to ping the URL. But you don't need to do that with every HTTP request you made from your App.

What you can do:

  1. Use below code to check connectivity

    private boolean checkInternetConnection()
    {
        ConnectivityManager cm = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
        // test for connection
        if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
  2. And while making rest call using HTTP client set timeout like 10 seconds. If you don't get response in 10 seconds means you donot have active internet connection and exception will be thrown (Mostly you get response within 10 seconds). No need to check active connection by pinging everytime (if you are not making Chat or VOIP app)

1

Maybe this can help you:

private boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
        // Test for connection
        if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    } 
    else {
        return false;
    }
}
2
  • 2
    This will only tell that there's a working active connection (wifi/mobile data), but not whether that connection actually provide internet access. It could be connected to a router without ISP connection, mobile data with no data credit, hotel wifi with no valid credential, restricted school/office lan, etc
    – Martheen
    Commented Jul 5, 2017 at 7:53
  • Then you should make an HTTP request either using volley or any other way and handle the response based on the response you check whether network is available or not. Commented Jul 5, 2017 at 8:20
1

Try this method, this will help you:

public static boolean isNetworkConnected(Context context)
{
    ConnectivityManager connectivityManager = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null)
    {
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected())
        {
            return true;
        }
    }
    return false;
}
1
  • 10
    You didn't get his point, this method will return true when WI-FI is connected with no internet connectivity i.e even on a Limited connection. Read Question carefully Commented Jul 5, 2017 at 6:36
1

You can try this for check Internet connectivity:

/**
 * Check Connectivity of network.
 */
public static boolean isOnline(Context context) {
    try {
        if (context == null)
            return false;

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm != null) {
            if (cm.getActiveNetworkInfo() != null) {
                return cm.getActiveNetworkInfo().isConnected();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    catch (Exception e) {
        Log.error("Exception", e);
        return false;
    }
}

In your activity you call this function like this.

if(YourClass.isOnline(context))
{
  // Do your stuff here.
}
else
{
  // Show alert, no Internet connection.
}

Don't forget to add ACCESS_NETWORK_STATE PERMISSION:

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

Try this if you want to just ping the URL:

public static boolean isPingAvailable(String myUrl) {
    boolean isAvailable = false;
    try {
        URL url = new URL(myUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        // 30 second time out.
        httpURLConnection.setConnectTimeout(30000);
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            isAvailable = true;
        }
    } catch (Exception e) {
        isAvailable = false;
        e.printStackTrace();
    }
    return isAvailable;
}
2
  • 1
    No it won't work properly , we need to ping any website to confirm the internet availability Commented Jul 5, 2017 at 7:04
  • 1
    In that case you have to use InetAddress. Look at here. Commented Jul 5, 2017 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.