【问题标题】:Change DNS servers of WIFI connection更改WIFI连接的DNS服务器
【发布时间】:2018-06-11 23:51:01
【问题描述】:

我正在开发一个需要在活动 Wi-Fi 连接上更改 DNS 服务器的应用。

我得到WiFiConfiguration 活动WiFi 连接的对象,并且我知道Java 的WiFiConfigurationlinkProperties 属性。但是Mono的WifiConfiguration不包含这个功能;因为它可以解决这种情况?

【问题讨论】:

  • 我认为你做不到。在我看来,这将是一个安全问题。但是你可以在代码中使用代理。

标签: android xamarin


【解决方案1】:

没有用于此的 API,但您可以使用反射来进行破解。不能保证这会奏效

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); 
    }

【讨论】:

  • 文档 (developer.android.com/reference/android/provider/…) 包含属性 WIFI_STATIC_DNS1。在 android 2 中可以使用它来设置 DNS 服务器 (stackoverflow.com/questions/4106502/…)。但是这个属性在 api17 中被弃用了。谷歌推荐使用 WiFiManager
  • 感谢您的回答!
  • 我之前通过反射尝试过 - 但 Xamarin 不允许这样做(不允许修改对象的字段)。而且,它是一种不可靠的方法,对于许多人来说是难以接受的程序......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
相关资源
最近更新 更多