如何在Android中以编程方式打开私有DNS设置?

pdkcd3nj  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(189)

是否可以在Android中使用编程打开私有DNS设置?

有没有办法以编程方式设置专用DNS值?

h79rfbju

h79rfbju1#

只能监听,不能主动切换私有域名的选择和设置,华为移动的也一直无法动态修改监听,代码如下:

val cMgr: ConnectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
    if (cMgr != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            cMgr.registerNetworkCallback(NetworkRequest.Builder().build(), object : ConnectivityManager.NetworkCallback() {
                @SuppressLint("MissingPermission")
                override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
                    super.onLinkPropertiesChanged(network, linkProperties)
                    println("onLinkPropertiesChanged(Network = $network, LinkProperties = $linkProperties)")
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                        println(
                            "LinkProperties.isPrivateDnsActive = " + linkProperties.isPrivateDnsActive()
                                .toString() + ", server =" + linkProperties.getPrivateDnsServerName()
                        )
                    }
                }
            })
        }
    }

相关问题