PackageCom.jason.demo.androidip;ImportAndroid.content.Context;ImportAndroid.net.DhcpInfo;ImportAndroid.net.wifi.WifiInfo;ImportAndroid.net.wifi.WifiManager;ImportAndroid.text.format.Formatter; Public classIPAddress { PublicString getipaddress (Context ctx) {//Get WiFi serviceWifimanager Wifimanager =(Wifimanager) Getsystemservice (Context.wifi_service); //determine if WiFi is turned on if not turned on this method will turn on WiFi if(!wifimanager.iswifienabled ()) {wifimanager.setwifienabled (true); } Wifimanager Wifi_service=(Wifimanager) Ctx.getsystemservice (Context.wifi_service); Dhcpinfo Dhcpinfo=Wifi_service.getdhcpinfo (); Wifiinfo Wifiinfo=Wifi_service.getconnectioninfo (); System.out.println ("Wifi Info----->" +wifiinfo.getipaddress ()); System.out.println ("DHCP Info Gateway----->" +formatter.formatipaddress (Dhcpinfo.gateway)); System.out.println ("DHCP info netmask----->" +formatter.formatipaddress (dhcpinfo.netmask)); //the IPAddress in Dhcpinfo is a variable of type int, which is converted to a string IP address by formatter returnformatter.formatipaddress (dhcpinfo.ipaddress); }}
The last word is to add permissions
Android: name=/>
Android: Name=
Android: name="Android.permission.INTERNET"/>
Detect the IP address of your Android phone