How to Get the Current Dns Servers for Android

How do I find the DNS servers in Android from a Java program?

The DNS protocol is not that complex - can't you just do the DNS accesses using raw sockets (either TCP or UDP)? After a quick look at the dnsjava doco it seems to provide low level DNS support to assist with this.

The other possible direction is, starting with dnsjava, to remove the dependence on /etc/resolv.conf. I would think about using getprop in your launch script to set properties in the JVM, or to create a localized resolv.conf file in your app's directory from which you can read the information needed. In other words, use getprop to inject information into the JVM instead of attempting to pull it in once the JVM is going. Surely creating a file that dnsjava can use directly should be doable.


Edit - android.net

It looks like android.net.ConnectivityManager will deliver you an array of NetworkInfo's using getAllNetworkInfo(). Then use android.net.NetworkUtils.runDhcp() to get a DhcpInfo for any given network interface - the DhcpInfo structure has the IP address for dns1 and dns2 for that interface. Surprised that the DNS's are int, therefore implying IP4 only, though.

how to get DNS Info in android

Here's a Project example that works:

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

Change DNS servers of WIFI connection

There is no API for that, but you can use reflection to do the hack. There is no guarantee that this will work

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

Xamarin forms GET DNS server from WIFI connection

I had done a simple to test your code and got the same null. And then I read the IPInterfaceProperties.DnsAddresses Property document, it seems that it doesn't support the xamarin.android.

You can check the it:https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipinterfaceproperties.dnsaddresses?view=net-6.0

In addition, I also search this but can not find any solution. So I try to use the android native method to get the dns address with the following code. So you can try to use the dependency service to get the dns address for android.

            ConnectivityManager cm = (ConnectivityManager)GetSystemService(ConnectivityService);
//NetworkInfo has been deprecated, but I can not find the new api to to this
NetworkInfo activeNetworkInfo = cm.ActiveNetworkInfo;
foreach (Network network in cm.GetAllNetworks())
{
NetworkInfo networkInfo = cm.GetNetworkInfo(network);
if (networkInfo.GetType() == activeNetworkInfo.GetType())
{
LinkProperties lp = cm.GetLinkProperties(network);
foreach (InetAddress addr in lp.DnsServers)
{
var a = addr.GetAddress();
}
}
}

How to set the Android emulator DNS server from Android Studio

Unfortunately, as of 3.0.1, this isn't possible. They removed adding additional arguments for emulators launched from Android Studio. Until they add it back in, starting the emulator from the command line (as you showed) is the only option.

You can track this issue here: https://issuetracker.google.com/issues/37071385



Related Topics



Leave a reply



Submit