android 获取IP
16lz
2022-04-17
[b]注意:下面这个方法不能在UI线程运行[/b]
private String getIP() {
String ipstr = "";
try {
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements())
{
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
String s = ip.getHostAddress();
if (!s.equals("127.0.0.1")) {
ipstr = s;
break;
}
}
}
if(ipstr.length()>0){
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipstr;
}
更多相关文章
- Android获取物理地址(支持5.0~10.0)
- Android(安卓)流媒体系列(二)
- Install Android(安卓)Composite ADB Interface driver for Wind
- AppCompat does not support the current theme features的解决
- Android(安卓)Dialog 的使用
- Android获取StatusBa有效高度
- Android中 Bitmap和Drawable相互转换的方法
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用