手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >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开发之SQLite的使用方法

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

在Android中访问WebService接口的方法

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

Android Mms之:PDU的使用详解

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

Android:序列化XML数据

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

android实现listview分页的方法

android中添加按钮事件的方法

精品推荐
分类导航