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()]);
    }
    
}
相关推荐
Chrome深度玩家3 小时前
谷歌翻译安卓版拍照翻译精准度与语音识别评测【轻松交流】
android·人工智能·语音识别
长点点3 小时前
从架构角度了解安卓APP(1):安卓核心组件的设计逻辑与演进
android·架构·app
用户71887350336803 小时前
Android适配最新SplashScreen方案
android·android jetpack
Hello.Reader4 小时前
洞悉 NGINX ngx_http_access_module基于 IP 的访问控制实战指南
tcp/ip·nginx·http
EQ-雪梨蛋花汤4 小时前
【Part 2安卓原生360°VR播放器开发实战】第二节|基于等距圆柱投影方式实现全景视频渲染
android·音视频·vr
Railshiqian4 小时前
Framework.jar里的类无法通过Class.forName反射某个类的问题排查
android·反射·framework.jar
鸿蒙布道师5 小时前
鸿蒙NEXT开发正则工具类RegexUtil(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
Anger重名了5 小时前
🌟 一篇文章搞懂Kotlin协程:比线程更轻量的并发神器
android
缘来的精彩6 小时前
adb常用的20个命令
android·adb·kotlin