在android下获取有线和无线IP地址的方法,经测试可用
16lz
2021-01-24
此方法来源于网络,简单修改后,测试通过,返回地址格式为“192.168.1.109”
public String getlocalIp() throws SocketException
{String ipaddress = null;
for (Enumeration
.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
if (intf.getName().toLowerCase().equals("eth0")
|| intf.getName().toLowerCase().equals("wlan0"))
{
for (Enumeration
.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
{
ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::"))
{// ipV6的地址
return ipaddress;
}
}
}
} else
{
continue;
}
}
return ipaddress;
}
更多相关文章
- 简叙 Activity 生命周期及android 返回按钮扑捉
- Android中在fragment A里面点击button跳转到fragment B实现方法
- Android(安卓)Activity 测试
- 圆角android
- Android(安卓)Service 测试
- framework——应用进程启动流程
- Android(安卓)几种图像特效处理的集锦
- 搜集的android资源