1

可能重复:
您如何获取 Android 的当前 DNS 服务器?

我想在 android 中获取 dns 服务器。这个线程说有一些 android.net.NetworkUtils 类,但我发现没有这样的类。在这里问了同样的问题,但没有解决。

4

2 回答 2

3

这是一个有效的项目示例:

https://github.com/rorist/android-network-discovery/blob/master/src/info/lamatricexiste/network/DnsDiscovery.java

for (long i = start; i < end + 1; i++) {
    hosts_done++;
    HostBean host = new HostBean();
    host.ipAddress = NetInfo.getIpFromLongUnsigned(i);
    try {
        InetAddress ia = InetAddress.getByName(host.ipAddress);
        host.hostname = ia.getCanonicalHostName();
        host.isAlive = ia.isReachable(timeout) ? 1 : 0;
    } catch (java.net.UnknownHostException e) {
        Log.e(TAG, e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    if (host.hostname != null && !host.hostname.equals(host.ipAddress)) {
        // Is gateway ?
        if (discover.net.gatewayIp.equals(host.ipAddress)) {
            host.deviceType = 1;
        }
        // Mac Addr
        host.hardwareAddress = HardwareAddress.getHardwareAddress(host.ipAddress);
        // NIC vendor
        try {
            host.nicVendor = HardwareAddress.getNicVendor(host.hardwareAddress);
        } catch (SQLiteDatabaseCorruptException e) {
            Log.e(TAG, e.getMessage());
        }
        publishProgress(host);
    } else {
        publishProgress((HostBean) null);
于 2012-01-12T18:28:02.757 回答
0

实际上,这看起来是一个很好的答案:

您如何获取当前适用于 Android 的 DNS 服务器?

android.net.ConnectivityManager将为您提供一系列 NetworkInfo 的 using getAllNetworkInfo(). 然后用于 android.net.NetworkUtils.runDhcp()获取任何给定网络接口的 DhcpInfo -DhcpInfo structure具有该接口的 IP 地址dns1dns2该接口的 IP 地址(它们是表示 IP 地址的整数值)。

这是另一个很好的链接:

http://www.devdaily.com/java/jwarehouse/android/wifi/java/android/net/wifi/WifiStateTracker.java.shtml

于 2012-01-12T18:33:33.517 回答