content: package com.allyn.lives.utils;
  
  import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  
  /**
   * Created by C on 21/3/2016.
   * https://github.com/nukc
   */
  public class NetworkUtils {
  
      /**
       * 判断当前网络是否是移动数据网络.
       *
       * @param context the context
       * @return boolean
       */
      public static boolean isMobile(Context context) {
          ConnectivityManager connectivityManager = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
          if (activeNetInfo != null
                  && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
              return true;
          }
          return false;
      }
  
  
      /**
       * 检查是否有wifi
       * @param context Context
       * @return boolean
       */
      public static boolean isWifi(Context context) {
          ConnectivityManager mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo info = mConnectivity.getActiveNetworkInfo();
          if (info == null) {
              return false;
          }
          int netType = info.getType();
          return netType == ConnectivityManager.TYPE_WIFI && info.isConnected();
      }
  
      /**
       * 描述：判断网络是否有效.
       *
       * @param context the context
       * @return true, if is network available
       */
      public static boolean isNetworkAvailable(Context context) {
          try {
              ConnectivityManager connectivity = (ConnectivityManager) context
                      .getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null) {
                  NetworkInfo info = connectivity.getActiveNetworkInfo();
                  if (info != null && info.isConnected()) {
                      if (info.getState() == NetworkInfo.State.CONNECTED) {
                          return true;
                      }
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
              return false;
          }
          return false;
      }
  }
  