Android 获取手机本地IP
16lz
2021-01-23
private String getLocalIPAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterface(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElements();
if ( !inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
// 这里增加限定条件inetAddress instanceof Inet4Address主要是在Android4.0高版本中可能优先得到的是IPV6地址
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("getIP", ex.toString());
ex.printStackTrace();
}
return null;
}
更多相关文章
- ionic 自动编译android的release版本
- Android版本28使用http请求报错not permitted by network securi
- Android android 6.0权限校验及版本兼容问题
- Android App 版本更新
- android 对比版本号
- android官方下载链接最新版 (Windows版本)
- android 版本更新和下载安装 适配android 7.0
- Android获取已安装应用信息(图标,名称,版本号,包)
- Android的版本的介绍