content: package com.rsen.util;
  
  import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.net.wifi.WifiInfo;
  import android.net.wifi.WifiManager;
  import android.telephony.TelephonyManager;
  import android.util.Log;
  
  import java.net.InetAddress;
  import java.net.NetworkInterface;
  import java.net.SocketException;
  import java.util.Enumeration;
  
  /**
   * 网络状态工具类
   * Created by angcyo on 15-12-16 016 15:26 下午.
   */
  public class NetUtil {
  
      /**
       * The constant TAG.
       */
      public static final String TAG = NetUtil.class.getSimpleName();
      /**
       * The constant MOBILE_CTWAP.
       */
      public static final String MOBILE_CTWAP = "ctwap";
      /**
       * The constant MOBILE_CMWAP.
       */
      public static final String MOBILE_CMWAP = "cmwap";
      /**
       * The constant MOBILE_3GWAP.
       */
      public static final String MOBILE_3GWAP = "3gwap";
      /**
       * The constant MOBILE_UNIWAP.
       */
      public static final String MOBILE_UNIWAP = "uniwap";
  
      /**
       * 获取当前网络类型
       *
       * @param context the context
       * @return 返回网络类型 network type
       */
      public static NetworkType getNetworkType(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
          final NetworkInfo info = cm.getActiveNetworkInfo();
          if (info == null || !info.isConnectedOrConnecting()) {
              return NetworkType.NONE;
          }
          int type = info.getType();
          if (ConnectivityManager.TYPE_WIFI == type) {
              return NetworkType.WIFI;
          } else if (ConnectivityManager.TYPE_MOBILE == type) {
              return NetworkType.MOBILE;
          } else {
              return NetworkType.OTHER;
          }
      }
  
      /**
       * 获取网络的名字
       *
       * @param context the context
       * @return the net type name
       */
      public static String getNetTypeName(Context context) {
          ConnectivityManager connectivityManager = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivityManager != null) {
              NetworkInfo activeNetworkInfo = connectivityManager
                      .getActiveNetworkInfo();
              if (activeNetworkInfo == null) {
                  return "No Net";
              }
              if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                  return activeNetworkInfo.getTypeName();
              } else {
                  String typeName = activeNetworkInfo.getSubtypeName();
                  if (typeName == null || typeName.length() == 0) {
                      return "Unknown Net";
                  } else if (typeName.length() > 3) {
                      return activeNetworkInfo.getSubtypeName().substring(0, 4);
                  } else {
                      return activeNetworkInfo.getSubtypeName().substring(0,
                              typeName.length());
                  }
              }
          } else {
              return "No Net";
          }
      }
  
      /**
       * Gets network type name.
       *
       * @param context the context
       * @return the network type name
       */
      public static String getNetworkTypeName(Context context) {
          String result = "(No Network)";
  
          try {
              final ConnectivityManager cm = (ConnectivityManager)
                      context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (cm == null) {
                  return result;
              }
  
              final NetworkInfo info = cm.getActiveNetworkInfo();
              if (info == null || !info.isConnectedOrConnecting()) {
                  return result;
              }
  
              result = info.getTypeName();
              if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                  result += info.getSubtypeName();
  //                        + "(" + info.getExtraInfo() + ")";
              } else {
  //                result += "(" + info.getExtraInfo() + ")";
              }
          } catch (Throwable ignored) {
          }
  
          return result;
      }
  
      /**
       * Gets operator.
       *
       * @param context the context
       * @return the operator
       */
      public static String getOperator(Context context) {
          TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          return tm.getNetworkOperator();
      }
  
      /**
       * 网络连接是否断开
       *
       * @param context Context
       * @return 是否断开s boolean
       */
      public static boolean isNotConnected(Context context) {
          return !isConnected(context);
      }
  
      /**
       * 是否有网络连接
       *
       * @param context Context
       * @return 是否连接 boolean
       */
      public static boolean isConnected(Context context) {
          if (context == null) {
              return true;
          }
          ConnectivityManager cm = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo info = cm.getActiveNetworkInfo();
          return info != null && info.isConnectedOrConnecting();
      }
  
      /**
       * 当前是否是WIFI网络
       *
       * @param context Context
       * @return 是否WIFI boolean
       */
      public static boolean isWifi(Context context) {
          return NetworkType.WIFI.equals(getNetworkType(context));
      }
  
      /**
       * 当前是否移动网络
       *
       * @param context Context
       * @return 是否移动网络 boolean
       */
      public static boolean isMobile(Context context) {
          return NetworkType.MOBILE.equals(getNetworkType(context));
      }
  
      /**
       * 判断网络是否可以用
       *
       * @param context the con
       * @return the boolean
       */
      public static boolean isNetOk(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (cm == null)
              return false;
          NetworkInfo netInfo = cm.getActiveNetworkInfo();
          if (netInfo == null) {
              return false;
          }
          if (netInfo.isConnected()) {
              return true;
          }
          return false;
      }
  
      /**
       * 取得device的IP address
       * <p/>
       * 需要权限 android.permission.ACCESS_WIFI_STATE
       *
       * @param context
       * @return 返回ip
       */
      public static String getWifiIp(Context context) {
          WifiManager wifiManager = (WifiManager) context
                  .getSystemService(Context.WIFI_SERVICE);
          WifiInfo wifiInfo = wifiManager.getConnectionInfo();
          int ipAddress = wifiInfo.getIpAddress();
  
  // 格式化IP address，例如：格式化前：1828825280，格式化后：192.168.1.109
          String ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
                  (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
                  (ipAddress >> 24 & 0xff));
          return ip;
      }
  
      /**
       * 检查是否有网络
       */
      public static boolean isNetworkAvailable(Context context) {
          NetworkInfo info = getNetworkInfo(context);
          if (info != null) {
              return info.isAvailable();
          }
          return false;
      }
  
      private static NetworkInfo getNetworkInfo(Context context) {
  
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          return cm.getActiveNetworkInfo();
      }
  
      /**
       * 获取网络ip
       */
      public static String getIp(Context context) {
          if (isWifiConnected(context)) {
              return getWifiIp(context);
          } else {
              return getMobileIP();
          }
      }
  
      /**
       * 判断网络是否可用
       *
       * @param context
       * @return
       */
      public static boolean isNetworkOK(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
  
          NetworkInfo info = cm.getActiveNetworkInfo();
  
          if (info != null && info.isConnectedOrConnecting()) {
              return true;
          }
          return false;
      }
  
  
      /**
       * 判断wifi是否可用
       *
       * @param context
       * @return
       */
      public static boolean isWifiConnected(Context context) {
          if (context != null) {
              ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                      .getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                      .getNetworkInfo(ConnectivityManager.TYPE_WIFI); // 返回WIFI
              // 的连接info
              if (mWiFiNetworkInfo != null) {
                  return mWiFiNetworkInfo.isAvailable();
              }
          }
          return false;
      }
  
  
      /**
       * Get IP For mobile
       */
      public static String getMobileIP() {
          try {
              for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                  NetworkInterface intf = (NetworkInterface) en.nextElement();
                  for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                      InetAddress inetAddress = enumIpAddr.nextElement();
                      if (!inetAddress.isLoopbackAddress()) {
                          String ipaddress = inetAddress.getHostAddress().toString();
                          return ipaddress;
                      }
                  }
              }
          } catch (SocketException ex) {
              Log.e("getMobileIP", "Exception in Get IP Address: " + ex.toString());
          }
          return "";
      }
  
      /**
       * The enum Network type.
       */
      public enum NetworkType {
          /**
           * Wifi network type.
           */
          WIFI, /**
           * Mobile network type.
           */
          MOBILE, /**
           * Other network type.
           */
          OTHER, /**
           * None network type.
           */
          NONE
      }
  }
  