Android Development Two Ways of Accurately Obtaining IP Address of Mobile Phone

  • 2021-11-29 08:22:55
  • OfStack

Recently, I have seen many examples of obtaining IP addresses online, and found that many of them are not completely accurate. Here, I write 1 to obtain ip addresses in two ways.

For example, WeChat payment requires App to pass in IP address when making interface in the background. We need to judge whether it is network environment, WI-FI or 3G, so we need to obtain ip address of these two environments.

Step 1: The first is to judge the network environment:


String ip;
ConnectivityManager conMann = (ConnectivityManager) 
				this.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		
		if (mobileNetworkInfo.isConnected()) {
			ip = getLocalIpAddress();	
			System.out.println(" Local ip-----"+ip);
		}else if(wifiNetworkInfo.isConnected())
		{
			WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
		  int ipAddress = wifiInfo.getIpAddress();  
		  ip = intToIp(ipAddress); 
		  System.out.println("wifi_ip The address is ------"+ip);
		}	

If the connection is a mobile network, step 2, obtain the local ip address: getLocalIpAddress (); In this way, the ip address in ipv4 format is obtained.


public String getLocalIpAddress() { 
    try { 
      String ipv4; 
      ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); 
      for (NetworkInterface ni: nilist)  
      { 
        ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses()); 
        for (InetAddress address: ialist){ 
          if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))  
          {  
            return ipv4; 
          } 
        } 
  
      } 
  
    } catch (SocketException ex) { 
      Log.e("localip", ex.toString()); 
    } 
    return null; 
  } 

If the connection is WI-FI network, step 3, obtain WI-FI ip address: intToIp (ipAddress);


public static String intToIp(int ipInt) { 
	    StringBuilder sb = new StringBuilder(); 
	    sb.append(ipInt & 0xFF).append("."); 
	    sb.append((ipInt >> 8) & 0xFF).append("."); 
	    sb.append((ipInt >> 16) & 0xFF).append("."); 
	    sb.append((ipInt >> 24) & 0xFF); 
	    return sb.toString(); 
	  } 

Many codes on the Internet obtain the local ip of ipv6. In WeChat payment, this ip address cannot be adjusted to WeChat payment, with the code:


private String getlocalIp() {
	String ip;
	
		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()&&!inetAddress.isLinkLocalAddress()) { 
//	  	        	ip=inetAddress.getHostAddress().toString();
						System.out.println("ip=========="+inetAddress.getHostAddress().toString());
						return inetAddress.getHostAddress().toString(); 
					 
					} 
				} 
			} 
		} catch (SocketException ex) { 
			Log.e("WifiPreference IpAddress", ex.toString()); 	  	  
	} 
	return null;
}

This article mainly introduces two ways for Android to accurately obtain the address of mobile phone IP. For more information about the way for Android to obtain the address of mobile phone IP, please see the related links below


Related articles: