2013.11.19 ——— android 获取本地ip地址
16lz
2021-01-23
2013.11.19 ——— android 获取本地ip地址
有的时候 可能只获取ipv4的地址,所以 上面的判断需要加上
//获取ip地址 public static String getIpForNet() { 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 ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; } public static String getIpForWifi(Context context){ WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = Formatter.formatIpAddress(ipAddress); return ip; }
有的时候 可能只获取ipv4的地址,所以 上面的判断需要加上
inetAddress instanceof Inet4Address
更多相关文章
- Android SDK和最新ADT下载地址
- android 4.0 sdk直接下载地址
- android学习路线和环境搭建、推荐一个博客地址
- Android Studio下载地址[国内可下载]
- Android 底部地址选择弹框
- 读取Android设备的MAC地址
- Android获取当前WiFi的MAC地址-适配所有版本