content: package com.pxs.terminal.util;
  
  import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.net.wifi.WifiInfo;
  import android.net.wifi.WifiManager;
  
  import java.net.Inet4Address;
  import java.net.InetAddress;
  import java.net.NetworkInterface;
  import java.net.SocketException;
  import java.util.Enumeration;
  
  public class MyUtil {
      /**
       * @param context
       * @return int
       * @Title: getNetWorkState
       * @Description: 获取当前网络状态
       */
      public static int getNetWorkState(Context context) {
          final int network_none = -1;// 没有连接网络
  
          final int network_mobile = 0;// 移动网络
  
          final int network_wifi = 1;// 无线网络
  
          // 得到连接管理器对象
          ConnectivityManager connectivityManager = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
  
          NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
          if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
  
              if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                  return network_wifi;
              } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                  return network_mobile;
              }
          } else {
              return network_none;
          }
          return network_none;
      }
  
      /**
       * @return String
       * @Title: getIpAddress
       * @Description: 获取设备ip地址
       */
      public static String getIpAddress() {
          try {
              for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces(); enNetI
                      .hasMoreElements(); ) {
                  NetworkInterface netI = enNetI.nextElement();
                  for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                      InetAddress inetAddress = enumIpAddr.nextElement();
                      if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                          return inetAddress.getHostAddress();
                      }
                  }
              }
          } catch (SocketException e) {
              e.printStackTrace();
          }
          return "";
      }
  
  
      //将获取的int转为真正的ip地址,参考的网上的，修改了下
      private static String intToInet(int i) {
          return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
      }
  
      // wifi下获取本地网络IP地址（局域网地址）
      public static String getLocalIPAddress(Context context) {
          WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          if (wifiManager != null) {
              WifiInfo wifiInfo = wifiManager.getConnectionInfo();
              String ipAddress = intToInet(wifiInfo.getIpAddress());
              return ipAddress;
          }
          return "";
      }
  }
  