获取wifi 4G IP地址

WiFi 以及 GPRS 获取 ip 地址



public String getIp(){
//获取wifi服务
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
return ip;
}
private String intToIp(int i) {

    return (i & 0xFF ) + "." +
            ((i >> 8 ) & 0xFF) + "." +
            ((i >> 16 ) & 0xFF) + "." +
            ( i >> 24 & 0xFF) ;
}

public String getLocalIpAddress()
{
    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("WifiPreference IpAddress", ex.toString());
}
return null;
}

你可能感兴趣的:(获取wifi 4G IP地址)