任何人都可以告诉我如何在Android 6中以编程方式设置静态IP和网关?
Settings.System不再工作了,goolgle说WIFI_STATIC_IP在API级别17中已弃用,WifiManger而是使用.不幸的是我无法找到关于它的任何事情WifiManger和WifiConfiguration类.
如何使用Java(以编程方式)在Android Wi-Fi连接上设置ProxySettings和ProxyProperties?
由于ipAssignment,linkProperties,ProxySettings和ProxyProperties是Android 3.1及更高版本WifiConfiguration中的隐藏字段,我需要能够枚举该类并使用这些字段.
使用下面的链接代码示例后,我可以为特定的Wi-Fi连接设置静态IP地址,网关和DNS,但我还需要设置Wificonfiguration.ProxySettings.STATIC和ProxyProperties
请参阅Stack Overflow问题如何在Android 3.x或4.x上以编程方式配置静态IP地址,网络掩码,网关.
例如,
WifiConfiguration config = new WifiConfiguration(configuration);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, ""));
Run Code Online (Sandbox Code Playgroud)
寻找类似的东西:
setProxySettings("STATIC", wifiConf);
setProxyProperties("proxyserver.mine.com.au", 8080, ""); // Set Proxy server and port.
wifiManager.updateNetwork(wifiConf); //apply the setting
Run Code Online (Sandbox Code Playgroud)
使用coolypf中的以下代码.ipAssignment .ProxySettings和linkProperties被隐藏...
WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
manager.asyncConnect(this, new Handler());
if (!manager.isWifiEnabled()) return;
List<WifiConfiguration> configurationList = manager.getConfiguredNetworks();
WifiConfiguration configuration = null;
int cur = manager.getConnectionInfo().getNetworkId();
for (int i = 0; i < configurationList.size(); ++i)
{
WifiConfiguration wifiConfiguration = configurationList.get(i); …Run Code Online (Sandbox Code Playgroud) 我在一个项目上工作,应该有一个功能,Wifi如果用户想要它,设置静态IP地址(DNS,网络掩码,网关).
我的初始和实际解决方案是使用android.provider.Settings.System允许此功能的类,但此解决方案仅适用于Android 2.x设备.
这很好,绝对不是完全零,但是对于更高版本的Android操作系统来说它也很好.它不知道为什么它不起作用.
如果我使用这个简单的方法来检查实际状态:
public static final boolean hasStaticIp(Context c) {
try {
return Settings.System.getInt(c.getContentResolver(),
Settings.System.WIFI_USE_STATIC_IP) == 1;
}
catch (SettingNotFoundException e) {
Log.i(TAG, "Settings not found (" + e.geMessage() + ")");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
它对于Android 2.x和Android 4.x都返回true,但在第二种情况下,更改肯定没有反映在Wifi我发现这是一个小硬编码的解决方案,但它没有按预期工作.
是否有人面临同样的问题?
我会很高兴任何有效的解决方案以及root设备(也许是Linux中的一些命令),因为很容易检查手机是否有根的状态.
提前致谢.
在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) 如何在Android 5.x上以编程方式配置静态IP地址,网络掩码,网关,DNS以进行Wi-Fi连接?是否有一个开放的API(没有找到它)或隐藏可用于此?如果可能的话,请你举个例子吗?
我知道它可以在Android 4.0+上运行,但它在Android 5.0上不起作用
再次陷入同样的问题。
我发现我们可以像这样设置静态系统设置:
System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "1"); // to define it use static ip's
System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP,"192.168.1.15");
System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_NETMASK,"255.255.255.0");
System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS1,"192.168.1.1");
System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY,"192.168.1.1");
Run Code Online (Sandbox Code Playgroud)
但是没有成功!
我不知道何时设置这些设置?
我应该在创建wifi配置之前还是在保存wifi配置之后甚至在激活它之前或之后执行此操作?
但是,我已经尝试了所有可能的情况,当我检查Android WiFi设置时,发现它仍在DHCP上。
之前的问题(即如何在Android 3.x或4.x上以编程方式配置静态IP地址,网络掩码,网关)完全毁了我的android设备,现在它无法再打开其WiFi。
我还在HTC手机上尝试了静态IP,但没有成功,始终处于DHCP模式!
我需要调用“ 重新连接 ”命令吗?如果是,那用哪种方式?
android ×7
java ×3
android-wifi ×2
ipv4 ×2
wifimanager ×2
ip ×1
ipv6 ×1
networking ×1
static ×1
wifi ×1