posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
code 参考:https://www.cnblogs.com/alex-zhao/p/5254624.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
     * 获取DNS,优先返回本地网络DNS(插网线),其次选择Wifi网络的DNS
     * */
    public static String getLocalDNS(Context context) {
        String TAG = "WiFiUtil_getLocalDNS";
        Process cmdProcess = null;
        BufferedReader reader = null;
        String dnsIP = "";
        try {
            cmdProcess = Runtime.getRuntime().exec("getprop net.dns1");
            reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
            dnsIP = reader.readLine();
        } catch (Exception ex) {
            slog.e(TAG, "获取Local网络DNS异常", ex);
            return null;
        } finally {
            try {
                reader.close();
            } catch (IOException ex) {
                slog.e(TAG, "获取Local网络DNS,关闭BufferedReader异常", ex);
            }
            cmdProcess.destroy();
        }
        if (Func.IsNullOrEmpty(dnsIP)) {
            try {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                if (wifiManager != null) {
                    // 获取WIFI的DHCP数据
                    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
                    if (dhcpInfo != null) {
                        String dns1 = intToIp(dhcpInfo.dns1);
                        String dns2 = intToIp(dhcpInfo.dns2);
                        String dnsDef = "0.0.0.0";
                        if (dnsDef.equals(dns1)) {
                            dns1 = "";
                        }
                        if (dnsDef.equals(dns2)) {
                            dns2 = "";
                        }
                        if (!Func.IsNullOrEmpty(dns1) && !Func.IsNullOrEmpty(dns2)) {
                            dnsIP = dns1 + ";" + dns2;
                        } else {
                            dnsIP = dns1 + dns2;
                        }
                    }
                }
            } catch (Exception ex) {
                slog.e(TAG, "获取Wifi网络DNS异常", ex);
            }
        }
        return dnsIP;
    }

 intToIp

复制代码
/**
     * int转换ip地址
     */
    public static String intToIp(int address) {
        return ((address & 0xFF) + "." +
                ((address >>>= 8) & 0xFF) + "." +
                ((address >>>= 8) & 0xFF) + "." +
                ((address >>>= 8) & 0xFF));
    }
复制代码

 

posted on   邢帅杰  阅读(304)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2022-08-22 vue3/透传 Attributes/插槽 Slots/依赖注入/组合式函数
点击右上角即可分享
微信分享提示