Android gets wifi IP, subnet mask, gateway, DNS and other information

Source: Internet
Author: User
Tags dns2

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.