1. Get the code for the mobile phone IP address:
public static String getLocalIpAddress(){ try{ for (Enumeration<NetworkInterface> en = (); ();) { NetworkInterface intf = (); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); ();) { InetAddress inetAddress = (); if (!()) { return ().toString(); } } } }catch (SocketException e) { // TODO: handle exception ("WifiPreference IpAddress---error-" + ()); } return null; }
However, under 4.0, an IP address similar to fe80::b607:f9ff:fee5:487e will appear. This is the address of IPV6. We need to obtain the address of IPV4, so we need to add a judgment in the appeal code.
InetAddressUtils.isIPv4Address(());
2. The complete code is as follows:
public static String getLocalIpAddress(){ try{ for (Enumeration<NetworkInterface> en = (); ();) { NetworkInterface intf = (); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); ();) { InetAddress inetAddress = (); if (!() && InetAddressUtils.isIPv4Address(())) { return ().toString(); } } } }catch (SocketException e) { // TODO: handle exception ("WifiPreference IpAddress---error-" + ()); } return null; }
Get the current IP address of Android phone
public class NetWorkUtils { /** * Check if the network is available * * @param paramContext * @return */ public static boolean checkEnable(Context paramContext) { boolean i = false; NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext .getSystemService("connectivity")).getActiveNetworkInfo(); if ((localNetworkInfo != null) && (())) return true; return false; } /** * Convert the integer form of ip into ip form * * @param ipInt * @return */ public static String int2ip(int ipInt) { StringBuilder sb = new StringBuilder(); (ipInt & 0xFF).append("."); ((ipInt >> 8) & 0xFF).append("."); ((ipInt >> 16) & 0xFF).append("."); ((ipInt >> 24) & 0xFF); return (); } /** * Get the current IP address * * @param context * @return */ public static String getLocalIpAddress(Context context) { try { // for (Enumeration<NetworkInterface> en = NetworkInterface // .getNetworkInterfaces(); ();) { // NetworkInterface intf = (); // for (Enumeration<InetAddress> enumIpAddr = intf // .getInetAddresses(); ();) { // InetAddress inetAddress = (); // if (!()) { // return ().toString(); // } // } // } WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = (); int i = (); return int2ip(i); } catch (Exception ex) { return "I got an IP error!!! Please make sure it is WIFI, or please re-open the network!\n" + (); } // return null; } }
Get the native IP address and MAC address in Android
Through(), we will always get "127.0.0.1". To get the real network IP address, we need to use the following method:
First, create a new project, modify the file and increase user permissions, as follows:
<uses-permission android:name=""// Must write
<uses-permission android:name=".ACCESS_NETWORK_STATE"/>
<uses-permission android:name=".CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name=".ACCESS_WIFI_STATE"></uses-permission>// Must write
<uses-permission android:name=".CHANGE_WIFI_STATE"></uses-permission>
The main function code is as follows:
// Get the IP address of the machine public String getLocalHostIp() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); // traversal of the network interface used while (()) { NetworkInterface nif = ();// Get all IPs bound to each network interface Enumeration<InetAddress> inet = (); // traverse all IPs bound to each interface while (()) { InetAddress ip = (); if (!() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ipaddress = "Native IP is" + ":" + (); } } } } catch (SocketException e) { ("feige", "Failed to get local IP address"); (); } return ipaddress; } // Get the address of the Mac public String getLocalMac() { String mac = ""; // Get the wifi manager WifiManager wifiMng = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfor = (); mac = "The mac address of the machine is:" + (); return mac; }
Android to get the IP address of wifi
WifiManager wifimanage=(WifiManager)(Context.WIFI_SERVICE);//Get WifiManager //Check if wifi is enabled if(!()) { (true); } WifiInfo wifiinfo= (); String ip=intToIp(()); //Convert the obtained int to the real IP address, refer to the online one, and modify it private String intToIp(int i) { return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)? + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF ); }
OK, is that okay? Haha, don't forget to add permissions
<uses -permission="" android:name=".ACCESS_WIFI_STATE"></uses>
<uses -permission="" android:name=".CHANGE_WIFI_STATE"></uses>
Summary: You can compare the method of obtaining the mobile phone IP address on Android to avoid unnecessary problems during the programming process.