public class Mainactivity extends Activity {private Wifimanager my_wifimanager;private wifiinfo wifiinfo;private Dhcpinfo dhcpinfo;private TextView tvresult; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); My_wifimanager = ((WifiManager) Getsystemservice ("WiFi"));d Hcpinfo = My_wifimanager.getdhcpinfo (); wifiinfo = My_wifimanager.getconnectioninfo (); Tvresult = (TextView) Findviewbyid (R.id.tvresult);} @Overrideprotected void Onresume () {super.onresume (); StringBuilder sb = new StringBuilder () sb.append ("Network information:") Sb.append ("\nipaddress:" + Inttoip (dhcpinfo.ipaddress)); Sb.append ("\nnetmask:" + Inttoip (dhcpinfo.netmask)); Sb.append ("\ngateway:" + Inttoip (Dhcpinfo.gateway)); Sb.append ( "\nserveraddress:" + Inttoip (dhcpinfo.serveraddress)) Sb.append ("\ndns1:" + Inttoip (dhcpinfo.dns1)); Sb.append ("\ Ndns2: "+ Inttoip (dhcpinfo.dns2)); Sb.append (" \ n "); System.out.println (Inttoip (dhcpinfo.ipaddress)); SYSTEM.OUT.PRINTLN (intToip (Dhcpinfo.netmask)); System.out.println (Inttoip (Dhcpinfo.gateway)); System.out.println (Inttoip (dhcpinfo.serveraddress)); System.out.println (Inttoip (dhcpinfo.dns1)); System.out.println (Inttoip (DHCPINFO.DNS2)); System.out.println (dhcpinfo.leaseduration); Sb.append ("WiFi info:"); Sb.append ("\nipaddress:" + Inttoip ( Wifiinfo.getipaddress ())); Sb.append ("\nmacaddress:" + wifiinfo.getmacaddress ()); Tvresult.settext (sb.toString ());} Private String Inttoip (int paramint) {return (Paramint & 0xFF) + "." + (0xFF & Paramint >> 8) + "." + (0xFF & Paramint >> 16) + "." + (0xFF & Paramint >> 24);}}
Effects such as the following:
Note:
Android.net.NetworkUtils
/** * Convert a IPV4 address from an integer to an inetaddress. * @param hostaddress an int corresponding to the IPV4 address in network byte order */public static InetAddress I nttoinetaddress (int hostaddress) { byte[] addressbytes = {(byte) (0xFF & Hostaddress), (Byte) (0xFF & ( Hostaddress >> 8)), ( byte) (0xFF & (hostaddress >> +)), (Byte) (0xFF & (Hostaddress >> )}; try { return inetaddress.getbyaddress (addressbytes); } catch (Unknownhostexception e) { throw new Assertionerror (); } }
Android gets wifi IP, subnet mask, gateway, DNS and other information