title: How to check if mobile data is actually active or wifi is actually connected to any hot spot?
body: <p><strong>I am trying to check if there is an internet connectivity or if the device is actually connected to wifi.</strong> </p>
  
  <p><strong>Note:</strong> I am not trying to see if there is internet connectivity or if just the wifi is connected. I want to check if the device is actually receiving and sending the packets.</p>
  
  <p>This code snippet actually checks if there is a mobile data connectivity or if the wifi is enabled but it doesn't say if the connection is actually established. </p>
  
  <pre><code>public boolean isNetworkConnected(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo ni = cm.getActiveNetworkInfo();            
          if (ni == null) {
              // There are no active networks.
              Toast.makeText(context, "No Network", Toast.LENGTH_LONG).show();
              return false;
          } else
              return true;
  </code></pre>
  
  <p><strong>OR</strong></p>
  
  <pre><code>    boolean isConnected = ni != null &amp;&amp; ni.isConnectedOrConnecting();
  
      if(isConnected){
  
          return true;
  
      }
      else{
          Toast.makeText(context, "No Network", Toast.LENGTH_LONG).show();
          return false;
      }
  }
  </code></pre>
  
  <p><strong>or</strong></p>
  
  <pre><code>public boolean haveNetworkConnection(Context context) {
          boolean haveConnectedWifi = false;
          boolean haveConnectedMobile = false;
  
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo[] netInfo = cm.getAllNetworkInfo();
          for (NetworkInfo ni : netInfo) {
              if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                  if (ni.isConnected())
                      haveConnectedWifi = true;
              if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                  if (ni.isConnected())
                      haveConnectedMobile = true;
          }
          return haveConnectedWifi || haveConnectedMobile;
      }
  </code></pre>
  
  <p>All the above code snippets only checks if the wifi or data is enabled. It doesn't show if the connection is established. It will always return true if the wifi or data is enabled.</p>
  
  <p><strong>Again</strong>, my goal is to check is to check if the connection is established.</p>
  
  <p>Any help will be really helpful.. Thank's in advance..:)</p>
  
  <p><strong>This question has been answered. Thanks again.</strong></p>