title: Broadcastreceiver to detect network is connected
body: <p>I'm trying to get the moment where user connects to a network, then I thought a <code>BroadcastReceiver</code> is a good approach... The thing that I would like to do is, when user connects to a network know if this network has connection to Internet.</p>
  
  <p><strong>The main thing also is know if the WiFi requires <code>Browse Log in</code> Example : <a href="http://developer.android.com/intl/es/reference/java/net/HttpURLConnection.html" rel="noreferrer"><code>Handling Network Sign-On</code></a> documentation</strong></p>
  
  <h2>What I've tried so far?</h2>
  
  <p>I've changed my <code>BroadcastReceiver</code> to this </p>
  
  <pre><code>if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
  NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
  if (networkInfo != null &amp;&amp; networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
      Log.d("Network", "Internet YAY");
  } else if (networkInfo != null &amp;&amp; networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) {
      if (isNetworkOnline()) {
          Log.d(TAG, String.valueOf(tikis));
          NetTask TeInternet = new NetTask();
          TeInternet.execute("https://www.google.com");
  
  
      }
  }
  </code></pre>
  
  <p>The problem is that when I try to connect to a <code>WiFi</code> without <code>Internet Connection</code> the input is this :</p>
  
  <pre><code>D/Network﹕ Internet YAY
  D/Network﹕ Internet YAY
  D/Network﹕ Internet YAY
  D/RequiresLoginBroadcast﹕ 1 //this occurs sometimes
  </code></pre>
  
  <p>I've changed the <code>Inner Class</code> to this acording with the <a href="http://developer.android.com/intl/es/reference/java/net/HttpURLConnection.html" rel="noreferrer"><code>Handling Network Sign-On</code></a> documentation</p>
  
  <p><code>doInBackground()</code> method:</p>
  
  <pre><code>protected Boolean doInBackground(String...params) {
  boolean internetAccessExists = false;
  String urlToBeAccessed = params[0];
  final int TIMEOUT_VALUE_IN_MILLISECONDS = 15000;
  URL url;
  HttpURLConnection urlConnection = null;
  try {
      url = new URL(urlToBeAccessed);
      urlConnection = (HttpURLConnection) url.openConnection();
      //set the respective timeouts for the connection
      urlConnection.setConnectTimeout(TIMEOUT_VALUE_IN_MILLISECONDS);
      urlConnection.setReadTimeout(TIMEOUT_VALUE_IN_MILLISECONDS);
      //the redirect check is valid only after the response headers have been received
      //this is triggered by the getInputStream() method
      InputStream in = new BufferedInputStream(urlConnection.getInputStream());
      if (!url.getHost().equals(urlConnection.getURL().getHost())) {
          internetAccessExists = true;
      }
  }
  //any sort of exception is considered as a redirect.
  //more specific exceptions such as SocketTimeOutException and IOException can be handled as well
  catch (Exception e) {
      Log.d(TAG, e.toString());
  } finally {
      Log.d(TAG, "Finally");
      urlConnection.disconnect();
  }
  return internetAccessExists;
  </code></pre>
  
  <h2>What I've looked for so far?</h2>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/5888502/how-to-detect-when-wifi-connection-has-been-established-in-android">How to detect when WIFI Connection has been established in Android?</a></li>
  <li><a href="https://stackoverflow.com/questions/9353005/android-wifi-how-to-detect-when-specific-wifi-connection-is-available">Android WIFI How To Detect When Specific WIFI Connection is Available</a></li>
  <li><a href="https://stackoverflow.com/questions/24314577/broadcastreceiver-is-not-working-detect-if-wifi-is-connected">BroadcastReceiver is not working (detect if wifi is connected)</a></li>
  </ol>
  
  <p>And more... but saddly I didn't find the correct answer to me.</p>
  
  <h2>TL;DR</h2>
  
  <p>The thing that I'm trying to do is get the exact event that users connects to a network and then get a good method to detect if I can make a google ping or to check if is there connection to Internet <strong>(ONLY WITH WIFI, 3G CONNECTION IS NOT ALLOWED)</strong>, because the code that I'm using at the moment is failing sometimes...</p>
  
  <p>I think this is a good method to know if there is an <code>Internet Connection</code> since the thing that I want to know is detect if Wifi Requires Browser Login. </p>
  
  <p>We are almost done... But I don't get why is entering on the <code>BroadcastReceiver</code> 4 times or even 5.... and sometimes saying that there's <code>Internet connection</code> when there is not...</p>