如何在Android手机网络中更改Dns?

时间:2015-07-02 09:19:41

标签: android dns android-networking

例如我们可以使用此代码更改Dns或其他网络信息

public static void setDNS(InetAddress dns, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException,
        NoSuchFieldException, IllegalAccessException {

    Object linkProperties = getField(wifiConf, "linkProperties");
    if (linkProperties == null)
        return;

    ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>)getDeclaredField(
            linkProperties, "mDnses");
    mDnses.clear(); // or add a new dns address , here I just want to
                    // replace DNS1
    mDnses.add(dns);
}

我想改变Dns for 3G mobile netwrok?请帮帮我... 我试过这个,如果有人知道这个PLZ的任何课程告诉我,我会发现更多。

Context context = DnsCacheService.this.getBaseContext();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Object prop = cm.getClass().getMethod("getActiveLinkProperties").invoke(cm);
Collection<InetAddress>    currDnses = ((LinkProperties) prop).getDnsServers();
Collection<InetAddress> newDnses = new ArrayList();
InetAddress dnstoAdd = InetAddress.getByName("127.0.0.1");
newDnses.add(dnstoAdd);
newDnses.add(dnstoAdd);
//prop.setDnsServers(newDnses);

0 个答案:

没有答案