根据源代码JavaDoc中的注释,我找到了一个有效的解决方案
ConnectivityManager cMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cMgr != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
cMgr.registerNetworkCallback(new NetworkRequest.Builder().build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
super.onLinkPropertiesChanged(network, linkProperties);
L.d(TAG, "onLinkPropertiesChanged(Network = " + network + ", LinkProperties = " + linkProperties + ")");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
L.i(TAG, "LinkProperties.isPrivateDnsActive = " + linkProperties.isPrivateDnsActive() + ", server =" + linkProperties.getPrivateDnsServerName());
}
}
});
}