在 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 地址