checking for Internet access?
<p>I have a Service which downloads a list of files one by one. However, when the internet connection goes down or it is disconnected the downloading gets stuck. I did some research on internet connection checking and found some examples utilizing the ConnectivityManager class.</p>

<p>However, the ConnectivityManager class seems to be checking only if the phone is connected to a network using WIFI or MOBILE. It doesn't check whether there actually is internet access. So you could be connected to a network and still be unable to browse using HTTP. </p>

<p>Is there any way other than ConnectivityManager?</p>

<p>I'm using the following alternative:</p>

<pre><code>public boolean isOnline(){

        try{
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
            urlc.connect();
            urlc.setConnectTimeout(1000 * 2);
            if(urlc.getResponseCode() == 200){
                return true;
            }else{
            }
        }catch(Exception e){
            return false;
        }

        return true;
}
</code></pre>

<p>The problem with this approach is that it's too slow. Since I'm checking inside a while loop (where the downloading of the file is taking place) the download speed is decreased due to the calls to isOnline():</p>

<pre><code>while(..something){
     if(!isOnline())
      killService();
      return; 
}
</code></pre>

<p>Any other way of doing this or improving on that one</p>

<p><strong>EDIT</strong>:
I was thinking of running a thread that checks if the download is stuck within a specified amount of time such as 20 secs or 30secs and if it is stop the service (meaning that the internet is down).</p>

<pre><code>int downloadProgress;
Runnable checkNet = new Runnable(){
    public void run(){
       // code to check download using Timer - need help implementing this
    }
};
</code></pre>
