你如何为Android当前的DNS服务器? [英] How do you get the current DNS servers for Android?

查看:174
本文介绍了你如何为Android当前的DNS服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄个地址当前使用的DNS服务器,在我的应用程序,无论是我连接直通Wifi或移动。 该DhcpInfo对象应该提供这一点,但我要如何才能找到一个有效的DhcpInfo对象?

I'm trying to get hold of the addresses to the currently used DNS servers in my application, either I'm connected thru Wifi or mobile. The DhcpInfo object should provide this but how can I get hold of a valid DhcpInfo object?

推荐答案

呼唤 getRuntime()。EXEC 能<一href="http://stackoverflow.com/questions/8688382/runtime-exec-bug-hangs-without-providing-a-process-object/11362081"标题=hungs>挂您的应用程序。

android.net.NetworkUtils.runDhcp()造成不必要的网络请求。

android.net.NetworkUtils.runDhcp() cause unnecessary network requests.

所以我preFER做到这一点:

So I prefer to do this:

Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
Method method = SystemProperties.getMethod("get", new Class[] { String.class });
ArrayList<String> servers = new ArrayList<String>();
for (String name : new String[] { "net.dns1", "net.dns2", "net.dns3", "net.dns4", }) {
    String value = (String) method.invoke(null, name);
    if (value != null && !"".equals(value) && !servers.contains(value))
        servers.add(value);
}

这篇关于你如何为Android当前的DNS服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆