Android 获取网关 ip 和 DNS ip

参考下方 PingUtil.java 代码

java 复制代码
import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.util.LinkedList;

public class PingUtil {
	
	/**
	 * 获取网关(API>=29)
	 */
    public static String getGateway(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
        int gateway = dhcpInfo.gateway;
        return Formatter.formatIpAddress(gateway);
    }

    /**
     * 获取dns
     */
    public static String getDns() {

        String[] dnsServers = getDnsFromCommand();
        // 组装
        StringBuffer sb = new StringBuffer();
        if (dnsServers != null) {
            sb.append(dnsServers[0]);  //注意这里只会返回1个dns服务器ip
        }
        return sb.toString();
    }


    //通过 getprop 命令获取
    private static String[] getDnsFromCommand() {
        LinkedList<String> dnsServers = new LinkedList<>();
        try {
            Process process = Runtime.getRuntime().exec("getprop");
            InputStream inputStream = process.getInputStream();
            LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = lnr.readLine()) != null) {
                int split = line.indexOf("]: [");
                if (split == -1) continue;
                String property = line.substring(1, split);
                String value = line.substring(split + 4, line.length() - 1);
                if (property.endsWith(".dns")
                        || property.endsWith(".dns1")
                        || property.endsWith(".dns2")
                        || property.endsWith(".dns3")
                        || property.endsWith(".dns4")) {
                    InetAddress ip = InetAddress.getByName(value);
                    if (ip == null) continue;
                    value = ip.getHostAddress();
                    if (value == null) continue;
                    if (value.length() == 0) continue;
                    dnsServers.add(value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dnsServers.isEmpty() ? new String[0] : dnsServers.toArray(new String[dnsServers.size()]);
    }
    
}
相关推荐
蒸鱼Yuzheng4 小时前
Android 构建可复现性:APK 指纹、文件级差异与供应链审计
android·apk·devops·软件供应链·可复现构建
为思念酝酿的痛5 小时前
数据链路层--帧传输与链路控制
网络·网络协议·计算机网络·数据链路层
程序员z76 小时前
【从0开始学计算机网络】| HTTP 协议的进化史
网络协议·计算机网络·http
墨天梦7 小时前
D03_Compose列表与稳定身份
android·gitee·kotlin
萌新杰少7 小时前
Kuikly股票查看软件开发体验——SaiRen
android·kotlin·客户端
vilya7 小时前
把 Python 塞进 APK:Chaquopy 打包实践
android·python
用户92817267390167 小时前
Android Compose版本的AI组件库来了。
android·kotlin
知昂七昂7 小时前
00-02:AOSP 源码阅读环境与检索方法论源码剖析(Android 16 / aosp-main)
android
知昂七昂7 小时前
00-01:AOSP 源码仓库结构与 repo 工作流源码剖析(Android 16 / aosp-main)
android·google
Multipath7127 小时前
乾元通轨道交通多链路聚合通信方案
网络·网络协议·5g·智能路由器·信息与通信