title: Check internet in background - Android
body: <p>I have been doing some research and seen some debate of whether to use Broadcast Receiver or Alarm Manager, Im not sure what to use but here is what I am trying to do and have done.</p>
  
  <p>I have a method that will check if there is internet connection. And then updates the UI accordingly. </p>
  
  <pre><code>public void isNetworkAvailable() {
      ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  
      if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
          mLayout.removeView(noInternetView);
          checkInternetViewOpen = false;
      } else {
          if (!checkInternetViewOpen) {
              checkInternetViewOpen = true;
              mLayout.addView(noInternetView, params);
          }
      }
  }
  </code></pre>
  
  <p>And while I am in an activity that will be using the internet I want to run this once every few seconds to make sure the internet is still active. Like this</p>
  
  <pre><code>while (usingInternet) {
      //I need to make it wait
      isNetworkAvailable();
  
  }
  </code></pre>
  
  <p>I also need to be not on the main thread for this, so I can make it wait, and then I will adjust the updating the UI parts on the main tread.</p>
  
  <p>So how can I make this on a background thread? And what option should I use?</p>
  
  <p>Thanks for the help in advance.</p>