此方法来源于网络,简单修改后,测试通过,返回地址格式为“192.168.1.109”

public String getlocalIp() throws SocketException

{
String ipaddress = null;
for (Enumeration en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
if (intf.getName().toLowerCase().equals("eth0")
|| intf.getName().toLowerCase().equals("wlan0"))
{
for (Enumeration enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
{
ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::"))
{// ipV6的地址
return ipaddress;
}
}
}
} else
{
continue;
}
}
return ipaddress;
}

更多相关文章

  1. 简叙 Activity 生命周期及android 返回按钮扑捉
  2. Android中在fragment A里面点击button跳转到fragment B实现方法
  3. Android(安卓)Activity 测试
  4. 圆角android
  5. Android(安卓)Service 测试
  6. framework——应用进程启动流程
  7. Android(安卓)几种图像特效处理的集锦
  8. 搜集的android资源

随机推荐

  1. 界面开发中布局属性全面剖析
  2. Android(安卓)Power Management
  3. 7715平台修改BUG记录
  4. Android的MediaRecorder架构介绍
  5. Android(安卓)ImageView 总结
  6. Android中自定义SeekBar来控制音量,并与系
  7. Android(安卓)recovery UI实现分析
  8. 【Android开发基础】应用界面主题Theme使
  9. android MultiDex multiDex原理(一)
  10. [Android(安卓)Studio / NDK] 如何使用ja