Search code examples
javaandroidandroid-networking

Change DNS programmatically in Android


I'm writing an app to change the DNS of my Android device. I've successfully gotten the current DNS info out, I can't however figure out how to set it to the new values.

I've tried the following approach:

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

Didn't seem to change anything though.

(I'm sure the values I'm inputting aren't correct - as of now I just want to see some changes in my settings)


Solution

  • Did you add the permission? [Link]

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