SoFunction
Updated on 2025-04-09

Android to get the implementation code of mobile phone IP address

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) &amp;&amp; (()))
			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 &amp; 0xFF).append(".");
		((ipInt &gt;&gt; 8) &amp; 0xFF).append(".");
		((ipInt &gt;&gt; 16) &amp; 0xFF).append(".");
		((ipInt &gt;&gt; 24) &amp; 0xFF);
		return ();
	}

	/**
	  * Get the current IP address
	  *
	  * @param context
	  * @return
	  */
	public static String getLocalIpAddress(Context context) {
		try {
			// for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface
			// .getNetworkInterfaces(); ();) {
			// NetworkInterface intf = ();
			// for (Enumeration&lt;InetAddress&gt; 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:

Copy the codeThe code is 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&lt;NetworkInterface&gt; en = NetworkInterface
     .getNetworkInterfaces();
   // traversal of the network interface used   while (())
   {
    NetworkInterface nif = ();// Get all IPs bound to each network interface    Enumeration&lt;InetAddress&gt; inet = ();
    // traverse all IPs bound to each interface    while (())
    {
     InetAddress ip = ();
     if (!()
       &amp;&amp; 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 &amp; 0xFF)+ "." + ((i &gt;&gt; 8 ) &amp; 0xFF)? + "." + ((i &gt;&gt; 16 ) &amp; 0xFF) +"."+((i &gt;&gt; 24 ) &amp; 0xFF );
} 

OK, is that okay? Haha, don't forget to add permissions

Copy the codeThe code is as follows:

<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.