Android中获取装置的IP

Android中获取设备的IP在wifi环境下,可以通过WifiInfo来获取设备的ip当然也可通过jdk总的NetworkInterface

Android中获取设备的IP
在wifi环境下,可以通过WifiInfo来获取设备的ip



当然也可通过jdk总的NetworkInterface来获取的,就是遍历所有的网络接口,获取到非loopback ip
public String getLocalIpAddress() {try {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()) {return inetAddress.getHostAddress().toString();}}}} catch (SocketException ex) {Log.e("", ex.toString());}return null;}