手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >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将Bitmap对象保存到SD卡中的方法

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

Android开发之SQLite的使用方法

在Android中创建菜单项Menu以及获取手机分辨率的解决方法

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

android 比较靠谱的图片压缩

android实现listview分页的方法

Android读取对应的键值

精品推荐
分类导航