如何从代码中获取设备的IP地址?是否可以使用一些代码获取设备的IP地址?
3 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
大话西游666
TA贡献1817条经验 获得超14个赞
getHostAddresshashCode
getHostAddress : ***** IP=fe80::65ca:a13d:ea5a:233d%rmnet_sdio0
hashCodeFormatter : ***** IP=238.194.77.212
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ip = Formatter.formatIpAddress(inetAddress.hashCode());
Log.i(TAG, "***** IP="+ ip);
return ip;
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
return null;}- 3 回答
- 0 关注
- 893 浏览
添加回答
举报
0/150
提交
取消
