java 如何为新的 WiFi 配置设置静态 IP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12394997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a static IP for new WiFi configuration?
提问by Seraphim's
Again stuck on the same problem.
再次陷入同样的问题。
I have found around that we can set static system settings like this:
我发现我们可以像这样设置静态系统设置:
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");
But No Success!
但没有成功!
I don't understand that when to set these settings?
我不明白什么时候设置这些设置?
Should I do it before the wifi configuration creation or after saving the wifi configuration or even before activating it or after it?
我应该在创建 wifi 配置之前还是在保存 wifi 配置之后甚至在激活它之前或之后执行它?
However, I have tried the all possible cases from my side and when I check Android WiFi settings, I see it's still on DHCP.
但是,我已经尝试了所有可能的情况,当我检查 Android WiFi 设置时,我发现它仍然在 DHCP 上。
A previous question i.e. How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.xhas completely ruined my android device and now it can't switch ON its WiFi anymore.
上一个问题,即如何在 Android 3.x 或 4.x 上以编程方式配置静态 IP 地址、网络掩码、网关已经完全毁了我的 android 设备,现在它无法再打开其 WiFi。
I also tried static IP on my HTC phone and no success, its always in DHCP mode!
我也在我的 HTC 手机上尝试了静态 IP,但没有成功,它总是处于 DHCP 模式!
Do I need to call a "reconnect" command? If yes, then in which way?
我需要调用“重新连接”命令吗?如果是,那么以哪种方式?
采纳答案by Seraphim's
After more than a year, I give up with setting a static IP (or DHCP, DNS, ...). Simply it's not possible, or, better, it's not allowed (from an arbitrary application).
一年多后,我放弃了设置静态 IP(或 DHCP、DNS,...)。根本不可能,或者更好的是,不允许(来自任意应用程序)。
Someone says:
有人说:
"You could use NDK - this gives you low-level access to Linux under Android. Warning: don't expect this to be documented or supported. They might even ban you from Android Market (I know I would)"
“您可以使用 NDK - 这使您可以在 Android 下对 Linux 进行低级别访问。警告:不要指望这会被记录或支持。他们甚至可能会禁止您进入 Android Market(我知道我会)”
For those who want to have some expriences with NDK, here is the a link:
对于那些想对 NDK 有一些经验的人,这里是一个链接:
http://developer.android.com/tools/sdk/ndk/index.html
http://developer.android.com/tools/sdk/ndk/index.html
Good luck, and give some feedback if you find something interesting!
祝你好运,如果你发现一些有趣的东西,请提供一些反馈!
回答by Zedot
I think you it should look like:
我认为你应该看起来像:
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_IP,"192.168.1.15");
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_DNS1, "192.168.1.1");
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.1.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_USE_STATIC_IP, "1");
And don't forget the manifest:
并且不要忘记清单:
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Regarding the WiFi problem that your device had, you can try switching the WIFI on programmatically. This post might be helpful: How to programmatically turn off WiFi on Android device?
关于您的设备的 WiFi 问题,您可以尝试以编程方式打开 WIFI。这篇文章可能会有所帮助: 如何以编程方式关闭 Android 设备上的 WiFi?