Java – Programmatically change DNS in Android

Programmatically change DNS in Android… here is a solution to the problem.

Programmatically change DNS in Android

I’m writing an app to change the DNS of my Android device.
I’ve successfully fetched the current DNS information, but I don’t know how to set it to a new value.

I tried the following:

android.provider.Settings.System.putString(v.getContext().getContentResolver(),android.provider.Settings.System.WIFI_USE_STATIC_IP, "192.168.100.102");
android.provider.Settings.System.putString(v.getContext().getContentResolver(),android.provider.Settings.System.WIFI_STATIC_DNS1, "192.168.0.2");
android.provider.Settings.System.putString(v.getContext().getContentResolver(),android.provider.Settings.System.WIFI_STATIC_DNS2, "192.168.0.3");
android.provider.Settings.System.putString(v.getContext().getContentResolver(),android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.1");
android.provider.Settings.System.putString(v.getContext().getContentResolver(),android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
android.provider.Settings.System.putString(v.getContext().getContentResolver(),android.provider.Settings.System.WIFI_STATIC_IP, "1");

Although nothing seems to have changed.

(I’m sure I entered the value incorrectly – so far I’m just looking at some changes in my settings).

Solution

Did you add permissions? [Link]

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

Related Problems and Solutions