title: Internet connection detection in android in an AsyncTask
body: <p>I have this class that I'm using for detecting in every Activity if there is internet connection. My problem is that I don't want to loop every time over the AsyncTask , because I got a problem in my progressDialog when I change the context. This is my class:</p>
  
  <p>public class InternetConnection {</p>
  
  <pre><code>public Boolean isInternetPresent;
  public ConnectionDetector cd;   
  public Context con;
  public ProgressDialog progressBar;
  
  public InternetConnection () {
      this.isInternetPresent = false;
  }
  
  public void hay_internet(){
  
      new Async().execute();
  
  }
  
  public void cambiar_context(Context c) {
      this.con = c;
      this.cd = new ConnectionDetector(c);
      progressBar=new ProgressDialog(con);
      progressBar.setCancelable(false);
      progressBar.setMessage("Buscando conexión de internet");
      progressBar.setTitle("Conectando ...");
      progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progressBar.setProgress(0);
  }
  
  class Async extends AsyncTask&lt;String, Integer, String&gt; {
  
       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           isInternetPresent = cd.isConnectingToInternet();
  
           if(!isInternetPresent) {                
               progressBar.show();
          }
       }
  
      @Override
      protected String doInBackground(String... params) {
          while(!isInternetPresent){
              isInternetPresent = cd.isConnectingToInternet();
          }
          return null;
      }
  
      @Override
       protected void onPostExecute(String file_url) {
          progressBar.dismiss();
          new Async().execute(); 
       }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How can I fix this loop, so I can stop calling everytime the AsyncTask. </p>