Set Android IP,DNS,GATEWAY setting programmatically

How do I set wifi ip address, dns address, gateway from android java i.e programmatically, I didn’t find any method which has the capability to store the values.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can change system settings programatically.

First you need to request the ‘WRITE_SETTINGS’ permission in your ‘AndroidManifest.xml’:

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

Then you need to actually change the setting using the following code:

    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "0");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS1, "");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS2, "");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, "");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_NETMASK, "");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP, "1");

The current settings can be accessed via the same method but use ‘getString’ instead of ‘putString’.

For information about the settings option visit the reference here:
Settings.System | Android Developers

Solution 2

You can’t do this from an application.

Would you like applications on your phone to change phone’s settings at will?

Solution 3

Not sure it will help but it’s possible to manually set an alternative ip,gateway, dns etc for a particular access point in the Wireless settings. Whether you can do this automatically or using an intent is another question?

I just saw this which might be helpful

How can i call Wi-Fi settings screen from my application using Android

Solution 4

android.provider.Settings.System.putString is deprecated now
this is the new method
also i don’t know how to use this new method, if someone knows that please learn it to me too

Solution 5

the follow code can also do that:

    WifiManager mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
    int dns1 = dhcpInfo.dns1;
    int dns2 = dhcpInfo.dns2;

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply