您的位置:首页 > 其它

获取手机IP地址

2015-07-21 18:00 225 查看
获取手机IP地址

第一种:

public int getIpAddress() {

WifiManager wifiManager = (WifiManager) this.context

.getSystemService(Context.WIFI_SERVICE);

// 判断wifi是否开启

if (!wifiManager.isWifiEnabled()) {

wifiManager.setWifiEnabled(true);

}

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

int ipAddress = wifiInfo.getIpAddress();

return ipAddress;

}

private String intToIp(int i) {

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

+ "." + (i >> 24 & 0xFF);

}

第二种

public String getInetIpAddress() {

try {

for (Enumeration<NetworkInterface> en = NetworkInterface

.getNetworkInterfaces(); en.hasMoreElements();) {

NetworkInterface intf = en.nextElement();

for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr

.hasMoreElements();) {

InetAddress inetAddress = ipAddr.nextElement();

return inetAddress.getHostAddress();

}

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

转:http://www.cnblogs.com/crearo-ssy/archive/2012/08/16/2640612.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: