2021-06-30网络相关信息获取

1.获取mac地址
 /**
  * 获取mac地址
  **/
  public String getWlanMac() {
     String str="";
     String macSerial="";
     try {
          Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
          InputStreamReader ir = new InputStreamReader(pp.getInputStream());
          LineNumberReader input = new LineNumberReader(ir);

          for (;null != str;) {
               str = input.readLine();
               if (str != null) {
                   macSerial = str.trim();// 去空格
                   break;
               }
          }
     } catch (Exception ex) {
          ex.printStackTrace();
     }
 }    
2.获取ip地址
 /**
  *  获取ip
  **/
  public String getIp() {
      String ip = mContext.getResources().getString(R.string.defalut_txt); 
      android.net.NetworkInfo.State mobile = mConnectivityManager.getNetworkInfo(
                ConnectivityManager.TYPE_MOBILE).getState();
      // wifi
      android.net.NetworkInfo.State wifi = mConnectivityManager.getNetworkInfo(
                ConnectivityManager.TYPE_WIFI).getState();
        
      if (mobile == android.net.NetworkInfo.State.CONNECTED
              || mobile == android.net.NetworkInfo.State.CONNECTING) {
          ip = getLocalIpAddress();
      }
	  if (wifi == android.net.NetworkInfo.State.CONNECTED
              || wifi == android.net.NetworkInfo.State.CONNECTING) {
          
          wifiInfo = wifiManager.getConnectionInfo();
          int ipAddress = wifiInfo.getIpAddress();
          ip = (ipAddress & 0xFF) + "." +
               ((ipAddress >> 8) & 0xFF) + "." +
               ((ipAddress >> 16) & 0xFF) + "." +
               (ipAddress >> 24 & 0xFF);
      }
      return ip;
  }

 /**
  * @return 手机GPRS网络的IP
  **/
  private String getLocalIpAddress() {
      try {
           //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
           for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                     InetAddress inetAddress = enumIpAddr.nextElement();
                     if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {//获取IPv4的IP地址
                         return inetAddress.getHostAddress();
                     }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }
3.获取SN号
 /**
  * 获取sn号
  */
  public String getAndroidOsSystemProperties() {
     String ret;
     Method systemProperties_get = null;
     try {
          systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
          if ((ret = (String) systemProperties_get.invoke(null, "ro.serialno")) != null)
              return ret;
     } catch (Exception e) {
          e.printStackTrace();
     }
     return "";
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值