1获取IPV4地址
public String getLocalIPAddress(){ String address = "0"; try { Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while(enumeration.hasMoreElements()){ NetworkInterface networkInterface = enumeration.nextElement(); Enumeration<InetAddress> enumeration2 = networkInterface.getInetAddresses(); while(enumeration2.hasMoreElements()){ InetAddress inetAddress = enumeration2.nextElement(); if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){ address = inetAddress.getHostAddress().toString(); return address; } } } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } return address; }
2获取IPV6地址
public String getLocalIPAddress(){ String address = "0"; try { Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while(enumeration.hasMoreElements()){ NetworkInterface networkInterface = enumeration.nextElement(); Enumeration<InetAddress> enumeration2 = networkInterface.getInetAddresses(); while(enumeration2.hasMoreElements()){ InetAddress inetAddress = enumeration2.nextElement(); if(!inetAddress.isLoopbackAddress()){ address = inetAddress.getHostAddress().toString(); return address; } } } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } return address; }
时间: 2024-10-25 13:38:01