Set Android IP,DNS,GATEWAY setting programmaticall

2019-01-08 08:16发布

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.

4条回答
Evening l夕情丶
2楼-- · 2019-01-08 08:43

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

查看更多
放我归山
3楼-- · 2019-01-08 08:48

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;
查看更多
Ridiculous、
4楼-- · 2019-01-08 08:53

You can't do this from an application.

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

查看更多
成全新的幸福
5楼-- · 2019-01-08 09:00

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

查看更多
登录 后发表回答