Android开发之初级开发_获取WiFi IP地址

Context context;

    public GetIPDress(Context context)
    {
        super();
        this.context = context;
    }

    /* Get ip address */
    String getLocalIpAddress() throws UnknownHostException
    {
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String ssid = wifiInfo.getSSID();// 获得本机所链接的WIFI名称
        String IP;
        int ipAddress = wifiInfo.getIpAddress();
        IP = intToIp(ipAddress);
        return IP + "\n" + "WIFI名字:" + ssid;
    }

    // ip地址转化成字符串
    private String intToIp(int i)
    {
        return (i & 0xFF) + " . " + ((i >> 8) & 0xFF) + " . " + ((i >> 16) & 0xFF) + " . " + ((i >> 24) & 0xFF);
    }

 

posted @ 2014-09-12 16:48  loneliness__白色  阅读(143)  评论(0)    收藏  举报