Get wifi ip address or mobile network IP address for android development tools
Sometimes we need to get the IP address of WIFI to get the IP address of the mobile phone network. This is a tool class dedicated to solving this problem. Here we need two permissions:
The first permission is required to obtain the IP address of the Wi-Fi network, and the second permission is required to obtain the IP address of the mobile network. The Code is as follows:
Public class GetIPAddressUtil {public static String getWifiIP (Context context) {String ip = null; WifiManager wifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); if (wifiManager. isWifiEnabled () {WifiInfo wifiInfo = wifiManager. getConnectionInfo (); int I = wifiInfo. getIpAddress (); ip = (I & 0xFF) +. + (I> 8) & 0xFF) +. + (I> 16) & 0xFF) +. + (I> 24 & 0xFF);} return ip;} public static String getMobileIP () {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 () {return inetAddress. getHostAddress (). toString () ;}}} catch (SocketException ex) {Log. e (Oh, an error occurred ..., ex. toString () ;}return null ;}}