Question

There are a number of questions regarding how to detect network connectivity in Android. https://stackoverflow.com/a/4239019/90236 provides a very detailed answer and https://stackoverflow.com/a/8548926/90236 shows methods to identify the type of network to which one is connected.

We detect network connectivity using code similar to those answers like:

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

This lets us warn a user if they are offline. However, it doesn't help us identify if a user has limited connectivity. There are conditions of limited connectivity that have caused users to complain. These include:

  1. Users are connected to a home wifi network but the cable modem or Internet service is unavailable.

  2. Users are in a hotel or coffee shop and successfully connect to a wifi network, but the network requires that they login or agree to terms of service before they are given full network access.

In both of these situations our isNetworkAvailable() test returns true. The OS tells us we are connected to a network. However, the user does not have usable Internet connectivity.

Is there a good way to determine that the user has a fully functioning network connection?

I'm considering calling a lightweight web service to verify their connectivity and warn the user if the app doesn't get a valid return from the service. Is this overkill? or have others had to resort to similar techniques?

Was it helpful?

Solution

Is there a good way to determine that the user has a fully functioning network connection?

Try downloading something from somewhere. Ideally, the "somewhere" is your own server, to test connectivity between the device and your server.

Alas, there are no doesTehConnexionSuck() or iCanHazBandwidth() methods on ConnectivityManager. :-)

Is this overkill?

In the spirit of "Goldilocks and the Three Bears", I'd describe that as justrightkill.

We have to do the same sorts of shenanigans in Web apps too.

OTHER TIPS

For completeness, let me post some code that I've added. I think it is fairly generic HttpClient code to make an Http request and return true when the request is succesful. I used HttpHead because I don't care about the entity in the response and this should make it faster. This method is in a class called ConnectivityHelper and DefaultSettings is a class that has a bunch of useful constants defined.

public static boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo;

    try {
        netInfo = manager.getActiveNetworkInfo();
    } catch (Exception e) {
        netInfo = null;
    }

    return netInfo != null && netInfo.isConnected() && testConnection(DefaultSettings.TEST_CONNECTION_URL);
}


private static boolean testConnection(String url) {
    HttpClient httpClient = null;
    HttpHead request = null;
    HttpResponse response = null;
    int statusCode = 0;

    try {
        HttpParams myParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(myParams, DefaultSettings.CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(myParams, DefaultSettings.SO_TIMEOUT);

        httpClient = new DefaultHttpClient(myParams);

        request = new HttpHead(url);
        response = httpClient.execute(request);
        statusCode = response.getStatusLine().getStatusCode();

        if (statusCode != 200)
        {
            Log.e(ConnectivityHelper.class.getName(), String.format("testConnection not successful. For %s - Status code = %d", url, statusCode));
            return false;
        }
        return true;

    } catch(ClientProtocolException e) {
        Log.e(ConnectivityHelper.class.getName(), String.format("testConnection failure. For %s - Exception: %s.", url, e.toString()));

        if (request != null && !request.isAborted()) {
            request.abort();
        }

        return false;
    } catch(IOException e) {
        if (statusCode == 401) {
            Log.e(ConnectivityHelper.class.getName(), String.format("testConnection access denied. For %s - Status code = %d", url, statusCode));               
        } else {
            Log.e(ConnectivityHelper.class.getName(), String.format("testConnection failure. For %s - Exception: %s.", url, e.toString()));
        }

        if (request != null && !request.isAborted()) {
            request.abort();
        }

        return false;
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }           
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top