24

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
faheem
  • 251
  • 1
  • 2
  • 4

5 Answers5

33

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, "192.168.0.2");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS2, "192.168.0.3");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.1");
    android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
    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

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
Flexiweb
  • 345
  • 3
  • 7
  • 4
    @Flexiweb, Bro. Its not working for Android OS Ver 3 & above. Can you please help for that too. – Harpreet Feb 25 '13 at 12:12
  • This may work for 'putString' but I am trying on 5.1.1 to 'getString' and it is returning null for every one of these. – Jeff.H May 01 '17 at 22:51
  • Microsoft Defender is not using WRITE_SETTINGS permission and is able to change the DNS. – Duna Jun 19 '23 at 05:46
1

You can't do this from an application.

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

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 3
    yes i want my application to change wifi ip settings.. its not possible? – faheem Nov 05 '10 at 14:25
  • I think the point Peter is trying to make is that you shouldn't want arbitrary applications to change these settings. – Kyle P Nov 05 '10 at 14:28
  • ok, i wanna create an application that changes these n/w settings (ip,dns,gateway).. create different profiles of wifi config. and on single touch it changes the configruation..60% of my app has completed,just want a method or anything to change these settings – faheem Nov 05 '10 at 14:29
  • 1
    Yes we understand what you would like to do - it's not possible. Applications can't change phone settings. – Peter Knego Nov 05 '10 at 15:39
  • i heard everything is possible in linux as well as android :)) – faheem Nov 05 '10 at 15:54
  • 2
    Well there is some truth in this. You could use NDK - this gives you low-level access to Linux under Android. Warning: don't expect this to be documented or supported. They might even ban you from Android Market (I know I would). http://developer.android.com/sdk/ndk/index.html – Peter Knego Nov 05 '10 at 16:16
0

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

Community
  • 1
  • 1
Bear
  • 1,541
  • 3
  • 20
  • 32
0

android.provider.Settings.System.putString is deprecated now this is the new method https://developer.android.com/reference/android/net/wifi/WifiManager also i don't know how to use this new method, if someone knows that please learn it to me too

simone
  • 1
  • 2
-4

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;
user2913643
  • 107
  • 1
  • 4