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()]);
    }
    
}
相关推荐
汀、人工智能44 分钟前
报错error: RPC failed,curl 16 Error in the HTTP2 framing layer解决方法
网络·git·网络协议·rpc
消失的旧时光-19432 小时前
kotlin的密封类
android·开发语言·kotlin
就这个java爽!3 小时前
JAVA网络编程【基于TCP和UDP协议】超详细!!!
java·开发语言·网络·tcp/ip·udp·eclipse·idea
服装学院的IT男3 小时前
【Android 13源码分析】WindowContainer窗口层级-4-Layer树
android
一叶飘零_sweeeet3 小时前
为什么 Feign 要用 HTTP 而不是 RPC?
java·网络协议·http·spring cloud·rpc·feign
KookeeyLena73 小时前
动态IP与静态IP:哪种更适合用户使用?
网络·网络协议·tcp/ip
CCTV果冻爽4 小时前
Android 源码集成可卸载 APP
android
码农明明4 小时前
Android源码分析:从源头分析View事件的传递
android·操作系统·源码阅读
时之彼岸Φ5 小时前
Web:HTTP包的相关操作
网络·网络协议·http
秋已杰爱5 小时前
HTTP中的Cookie与Session
服务器·网络协议·http