Set Android IP,DNS,GATEWAY setting programmatically
34,206
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, "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
Solution 2
You can't do this from an application.
Would you like applications on your phone to change phone's settings at will?
Related videos on Youtube
Author by
faheem
Updated on July 09, 2022Comments
-
faheem almost 2 years
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.
-
faheem over 13 yearsyes i want my application to change wifi ip settings.. its not possible?
-
Kyle P over 13 yearsI think the point Peter is trying to make is that you shouldn't want arbitrary applications to change these settings.
-
faheem over 13 yearsok, 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
-
Peter Knego over 13 yearsYes we understand what you would like to do - it's not possible. Applications can't change phone settings.
-
faheem over 13 yearsi heard everything is possible in linux as well as android :))
-
faheem over 13 yearsThanks,by using intent is it possible to change wifi settings?
-
Peter Knego over 13 yearsWell 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). developer.android.com/sdk/ndk/index.html
-
Harpreet about 11 years@Flexiweb, Bro. Its not working for Android OS Ver 3 & above. Can you please help for that too.
-
Timothy Miller almost 8 yearsHow does this set the DNS value?
-
Jeff.H almost 7 yearsThis may work for 'putString' but I am trying on 5.1.1 to 'getString' and it is returning null for every one of these.