如何从android java设置wifi ip地址、dns地址、网关,即以编程方式,我没有找到任何能够存储值的方法.
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.
您可以通过编程方式更改系统设置.
You can change system settings programatically.
首先,您需要在AndroidManifest.xml"中请求WRITE_SETTINGS"权限:
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");
可以通过相同的方法访问当前设置,但使用getString"而不是putString".
The current settings can be accessed via the same method but use 'getString' instead of 'putString'.
有关设置选项的信息,请访问此处的参考:Settings.System |安卓开发者
For information about the settings option visit the reference here: Settings.System | Android Developers
这篇关于以编程方式设置 Android IP、DNS、GATEWAY 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!