Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Android IP,DNS,GATEWAY setting programmatically

Tags:

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.

like image 218
faheem Avatar asked Nov 05 '10 13:11

faheem


People also ask

Can I set DNS on Android?

Changing DNS on Android 9 and Higher To manually change the DNS, follow these steps: Open your phone's Settings, then tap Network & Internet. Tap Advanced. Tap Private DNS, then select Private DNS provider hostname and enter either the Cloudflare URL or one of the CleanBrowing URLs in the text field.


2 Answers

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

like image 104
Flexiweb Avatar answered Oct 22 '22 20:10

Flexiweb


You can't do this from an application.

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

like image 25
Peter Knego Avatar answered Oct 22 '22 19:10

Peter Knego