title: What is the best way to check internet connection in Android?
body: <p>I'm using these code in an <code>AsyncTask</code> to check Internet connection:</p>
  
  <pre><code>ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo netInfo = cm.getActiveNetworkInfo();
              if (netInfo != null &amp;&amp; netInfo.isConnected()) {
                  try {
                      URL url = new URL("http://www.google.com");
                      HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                      urlc.setConnectTimeout(3000);
                      urlc.connect();
                      if (urlc.getResponseCode() == 200) {
                          return true;
                      }
                  } catch (MalformedURLException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
              else {
      //toast show
              }
  </code></pre>
  
  <p>This code isn't working on some device like most of Huawei models, and Samsung Galaxy Note 3. </p>
  
  <p>Also, if user has Internet connection with data like GPRS ، EDGE ، 3G, 4G ... </p>
  
  <p>What is the best code to check internet connection to support on all device and all kind of connections?</p>