0

I'm trying to detect whether a device has a wifi or 3g connection. If it has a connection it will display the MapView, but I'm having problems with my conditional statement when neither of the two is connected.

Here's how I check for a network connection:

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;
}

Here is the code where I call the method. It displays the MapView when it has internet access, but it does not display the toast when it has no internet access.

if (haveNetworkConnection()) {
    if (loc != null && !loc.equals("")) {
        new GeocoderTask().execute(loc);
    } else {
        Toast.makeText(this, "Internet Connection is Required to view this map", Toast.LENGTH_LONG).show();
    }
}
0

4 Answers 4

0

Assuming your loop returns the correct values, let's take out the local booleans since they are unnecessary:

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

If the loop is incorrect, you can incorporate the documentation's method in your code:

private boolean haveNetworkConnection() {
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if(activeNetwork != null)
        return activeNetwork.isConnectedOrConnecting();
    return false;
}
0
0

Try this to check whether an internet connection is available:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

    return activeNetworkInfo != null;
}
0
0
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())
                return true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                return true;
    }
    return false;
}
0
0

Your logic is wrong in the following:

if (haveNetworkConnection()) {
        if (loc != null && !loc.equals("")) {
            new GeocoderTask().execute(loc);
        } else {
            Toast.makeText(this, "Internet Connection is Required to view this map", Toast.LENGTH_LONG).show();
        }
    }

If haveNetworkConnection () is false you'll never get to the Toast. You should change it as follows:

if (haveNetworkConnection()) {
            if (loc != null && !loc.equals("")) {
                new GeocoderTask().execute(loc);
            } 
        }
else {
                Toast.makeText(this, "Internet Connection is Required to view this map", Toast.LENGTH_LONG).show();
            }
0

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.