千锋教育-做有情怀、有良心、有品质的职业教育机构

手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

当前位置:首页  >  技术干货  > Android获取IP地址详解

Android获取IP地址详解

来源:千锋教育
发布人:xqq
时间: 2023-11-24 11:41:43 1700797303

在 Android 开发中,获取设备 IP 地址是一项非常常见的任务。Android 操作系统提供了多种方式来获取设备的 IP 地址,本文将介绍其中一些方法。

public static String getIPAddress() { try { List interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) { return addr.getHostAddress(); } } } } catch (Exception e) { Log.e("getIPAddress", e.toString()); } return ""; }

该方法返回设备的 IPv4 地址 如果设备没有网络连接,该方法将返回空字符串 该方法需要在 AndroidManifest.xml 中声明上述两个权限

public static String getWifiIPAddress(Context context) { WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); return Formatter.formatIpAddress(ipAddress); }

该方法返回当前连接的 Wifi 路由器的IPv4地址 如果设备没有连接 Wifi,该方法将返回 "0.0.0.0" 该方法需要在 AndroidManifest.xml 中声明上述两个权限

public static List getDNS(Context context) { List dnsList = new ArrayList(); WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiMgr.getDhcpInfo(); int dns1 = dhcpInfo.dns1; int dns2 = dhcpInfo.dns2; dnsList.add(Formatter.formatIpAddress(dns1)); dnsList.add(Formatter.formatIpAddress(dns2)); return dnsList; }

private void setStaticIpAddress(String ip, String gateway, String netmask, String dns1, String dns2) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wifiConfig = null; WifiInfo connectionInfo = wifiManager.getConnectionInfo(); List configuredNetworks = wifiManager.getConfiguredNetworks(); if (configuredNetworks != null) { String ssid = connectionInfo.getSSID(); for (WifiConfiguration conf : configuredNetworks) { if (ssid.equals(conf.SSID)) { wifiConfig = conf; break; } } if (wifiConfig != null) { try { setIpAssignment("STATIC", wifiConfig); setIpAddress(InetAddress.getByName(ip), Integer.parseInt(netmask), wifiConfig); setGateway(InetAddress.getByName(gateway), wifiConfig); setDNS(InetAddress.getByName(dns1), InetAddress.getByName(dns2), wifiConfig); wifiManager.updateNetwork(wifiConfig); wifiManager.enableNetwork(wifiConfig.networkId, true); wifiManager.reconnect(); } catch (Exception e) { e.printStackTrace(); } } } } private void setIpAssignment(String assign, WifiConfiguration wifiConf) throws Exception { WifiConfiguration.IpAssignment ipAssignment = WifiConfiguration.IpAssignment.valueOf(assign); wifiConf.ipAssignment = ipAssignment; Method setIpAssignment = WifiConfiguration.class.getMethod("setIpAssignment", WifiConfiguration.IpAssignment.class); setIpAssignment.invoke(wifiConf, ipAssignment); } private void setIpAddress(InetAddress addr, int prefixLength, WifiConfiguration wifiConf) throws Exception { Method setIpAddress = WifiConfiguration.class.getMethod("setIpAddress", InetAddress.class, int.class); setIpAddress.invoke(wifiConf, addr, prefixLength); } private void setGateway(InetAddress gateway, WifiConfiguration wifiConf) throws Exception { Method setGateway = WifiConfiguration.class.getMethod("setGateway", InetAddress.class); setGateway.invoke(wifiConf, gateway); } private void setDNS(InetAddress dns1, InetAddress dns2, WifiConfiguration wifiConf) throws Exception { List dnsList = new ArrayList(); dnsList.add(dns1); dnsList.add(dns2); Method setDns = WifiConfiguration.class.getMethod("setDnsServers", List.class); setDns.invoke(wifiConf, dnsList); }

该方法可以设置设备的静态 IP 地址,网关,子网掩码和 DNS 该方法需要在 AndroidManifest.xml 中声明上述两个权限

public static List getDNS(Context context) { List dnsList = new ArrayList(); WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiMgr.getDhcpInfo(); int dns1 = dhcpInfo.dns1; int dns2 = dhcpInfo.dns2; dnsList.add(Formatter.formatIpAddress(dns1)); dnsList.add(Formatter.formatIpAddress(dns2)); return dnsList; }

该方法返回当前连接的 Wifi 路由器的 DNS 列表 如果设备没有连接 Wifi,该方法将返回空列表 该方法需要在 AndroidManifest.xml 中声明上述两个权限

public static String getPublicIPAddress() { String ipAddress = ""; try { URL url = new URL("http://checkip.amazonaws.com"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5000); con.setReadTimeout(5000); con.setRequestMethod("GET"); InputStream in = new BufferedInputStream(con.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); ipAddress = reader.readLine(); reader.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } return ipAddress; }

该方法返回设备的公网 IP 地址 需要借助 http://checkip.amazonaws.com 来获取公网 IP 地址,也可以通过其他类似的第三方服务获取公网 IP 地址
声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。
10年以上业内强师集结,手把手带你蜕变精英
请您保持通讯畅通,专属学习老师24小时内将与您1V1沟通
免费领取
今日已有369人领取成功
刘同学 138****2860 刚刚成功领取
王同学 131****2015 刚刚成功领取
张同学 133****4652 刚刚成功领取
李同学 135****8607 刚刚成功领取
杨同学 132****5667 刚刚成功领取
岳同学 134****6652 刚刚成功领取
梁同学 157****2950 刚刚成功领取
刘同学 189****1015 刚刚成功领取
张同学 155****4678 刚刚成功领取
邹同学 139****2907 刚刚成功领取
董同学 138****2867 刚刚成功领取
周同学 136****3602 刚刚成功领取
相关推荐HOT