2

i used this code to check the internet 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;
}

but it doesn"t handle the case when I have a limited connectivity to the internet, how to fix it ?

user1708001
  • 63
  • 2
  • 5
  • Check [this link](http://stackoverflow.com/questions/6493517/android-detect-if-device-has-internet-connection) .It shows how to check for active network connections. – PrasadW Sep 29 '12 at 11:45

1 Answers1

0

Try to open a web link programmatically in background if you get a success status then internet connection is available else if it fails there is no internet connection available. Try HttpURLConnection to check the internet availability by opening any link, you can get HttpURLConnection details in given link : HttpURLConnection

Daud Arfin
  • 2,432
  • 1
  • 15
  • 34
  • ok but I have a list view with different video streams to launch, so if the user clicks on a item, I will always have to load an extra page and then load the video : it will take more time ... But if it's the only way to do it, I have no choice :p (maybe making a ping test will be faster ?) – user1708001 Sep 29 '12 at 08:40
  • It will be faster i guess so, and i am talking about ping test only using HttpURLConnection. – Daud Arfin Sep 29 '12 at 08:44