title: Proper way to check if Wifi is connected in android
body: <p>For my app, I need to make sure the user is connected  to wifi before contact with the server. I have found two methods to do so, but I am not sure if one suffices.</p>
  
  <p>First I am adding this:</p>
  
  <pre><code>WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext()
              .getSystemService(WIFI_SERVICE);
  if (!wifiManager.isWifiEnabled()) {
          buildAlertNoWifi();
          showProgressDialog(false, "");
          return;
  }
  </code></pre>
  
  <p>And then I am doing this:</p>
  
  <pre><code>ConnectivityManager cm = (ConnectivityManager) getActivity()
              .getSystemService(Context.CONNECTIVITY_SERVICE);
  
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      if (activeNetwork != null) { // connected to the internet
          if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
              // connected to wifi
  
          } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
              // connected to the mobile provider's data plan
              Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
              return;
          }
      } else {
          Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
          return;
      }
  </code></pre>
  
  <p>So I was wondering if <code>wifiManager.isWifiEnabled()</code> returns whether the device is <strong>connected</strong> to a wifi or just has wifi turned on. And if so, is it enough to use it alone? </p>