如何从代码中获取设备的IP地址?是否可以使用一些代码获取设备的IP地址?
3 回答
![?](http://img1.sycdn.imooc.com/5333a207000118af02200220-100-100.jpg)
宝慕林4294392
TA贡献2021条经验 获得超8个赞
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
![?](http://img1.sycdn.imooc.com/545861f00001be3402200220-100-100.jpg)
大话西游666
TA贡献1817条经验 获得超14个赞
getHostAddress
hashCode
getHostAddress
: ***** IP=fe80::65ca:a13d:ea5a:233d%rmnet_sdio0
hashCode
Formatter
: ***** 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 关注
- 842 浏览
添加回答
举报
0/150
提交
取消