我已经检查了Stack Overflow问题API,以便在Android应用程序中配置静态IP地址.
它适用于Android 2.3.但是,在更高的API级别上没有运气.例如,我把设置
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "1");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP, "192.168.0.100");
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_DNS1, "192.168.0.254");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.254");
Run Code Online (Sandbox Code Playgroud)
但我回去查看:
Setting --> Wi-Fi --> Long Press Access Point SSID --> Modify Network --> check Show advanced options
Run Code Online (Sandbox Code Playgroud)
该IP Settings领域仍然陈述DHCP但不是Static.
确实,我可以android.provider.Settings.System.getString()用来取回我设定的东西.它证明设置保存在某处,但系统只是忽略它.
系统使用android.provider.Settings.SystemAndroid 3.x和4.x以外的设置,因为设置是根据接入点SSID设置的.我可以修改一个SSID上的设置,就像它在Android 2.3上的工作方式一样吗?
任何人都可以告诉我如何在Android 6中以编程方式设置静态IP和网关?
Settings.System不再工作了,goolgle说WIFI_STATIC_IP在API级别17中已弃用,WifiManger而是使用.不幸的是我无法找到关于它的任何事情WifiManger和WifiConfiguration类.
在Android 4.0.2..4.4.4上运行的代码存在一些问题,但是在Android 5上并没有真正起作用,我不知道为什么.基本上,下面的代码允许设置新WiFi的IP分配类型:STATIC或DHCP.我在这个答案中详细介绍了我使用的代码:https://stackoverflow.com/a/10309323/876360
我将尝试使用输出信息将最重要的代码放在这里.
...
WifiConfigurator.setIpAssignment("STATIC", wifiConf);
...
Run Code Online (Sandbox Code Playgroud)
wifiConf是哪里
public static WifiConfiguration getCurrentWiFiConfiguration(Context context) {
WifiConfiguration wifiConf = null;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
if(configuredNetworks != null){
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == connectionInfo.getNetworkId()) {
wifiConf = conf;
break;
}
}
}
}
}
return …Run Code Online (Sandbox Code Playgroud)