手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >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地址的问题】相关文章:

android 比较靠谱的图片压缩

android动态壁纸调用的简单实例

android 开发教程之日历项目实践(二)

Android利用方向传感器获得手机的相对角度实例说明

android实现获取正在运行的应用程序

android自动化测试中实现长按并拖动

Android开发:内存机制分析——堆和栈

10个常见的Android开发新手误区

Android动态添加View的问题解决方法

android中添加按钮事件的方法

精品推荐
分类导航