手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android4.0 获取手机IP地址的问题
android4.0 获取手机IP地址的问题
摘要:获取手机IP地址的代码:publicstaticStringgetLocalIpAddress(){try{for(Enumeratione...

获取手机IP地址的代码:

public static 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()) { return inetAddress.getHostAddress().toString(); } } } }catch (SocketException e) { // TODO: handle exception Utils.log("WifiPreference IpAddress---error-" + e.toString()); } return null; }

但是在4.0 下 会出现类似fe80::b607:f9ff:fee5:487e的IP地址, 这个是IPV6的地址,我们需要获得是的IPV4的地址,所以要在上诉代码中加一个判断

InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())

完整代码如下:

public static 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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress().toString(); } } } }catch (SocketException e) { // TODO: handle exception Utils.log("WifiPreference IpAddress---error-" + e.toString()); } return null; }

【android4.0 获取手机IP地址的问题】相关文章:

Mac 下搭建Android开发环境

android将Bitmap对象保存到SD卡中的方法

android实现listview分页的方法

Fedora14下android开发: eclipse与ibus确有冲突的问题分析

android中添加按钮事件的方法

Android获取屏幕方向及键盘状态的小例子

android开发环境遇到adt无法启动的问题分析及解决方法

Android开发之SQLite的使用方法

Android获取通话时间实例分析

android WebView组件使用总结

精品推荐
分类导航