Android obtains the IP address of the device.
Overall retrieval: You can use mobile data to access the Internet and obtain IP addresses. You can also use WiFi to obtain IP addresses.
public static String getPhoneIp() { try { for (Enumeration
en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration
enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { // if (!inetAddress.isLoopbackAddress() && inetAddress // instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { } return "127.0.0.1"; }
How to obtain IP addresses through Wifi
Private String intToIp (int ip) {return (ip & 0xFF) + ". "+ (ip> 8) & 0xFF) + ". "+ (ip> 16) & 0xFF) + ". "+ (ip> 24) & 0xFF);} private String getIp () {// get wifi service WifiManager wifiManager = (WifiManager) getSystemService (Context. WIFI_SERVICE); // determines whether wifi is enabled if (wifiManager. isWifiEnabled () {// wifiManager. setWifiEnabled (true); WifiInfo wifiInfo = wifiManager. getConnectionInfo (); int ipAddress = wifiInfo. getIpAddress (); return intToIp (ipAddress);} return "127.0.0.1 ";}