以编程方式设置 Android IP、DNS、GATEWAY 设置

发布于 2024-09-30 23:48:34 字数 74 浏览 0 评论 0原文

如何以编程方式从 android java ie 设置 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兔姬 2024-10-07 23:48:35

下面的代码也可以做到这一点:

    WifiManager mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
    int dns1 = dhcpInfo.dns1;
    int dns2 = dhcpInfo.dns2;

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;
风启觞 2024-10-07 23:48:34

您可以通过编程方式更改系统设置。

首先,您需要在“AndroidManifest.xml”中请求“WRITE_SETTINGS”权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

然后您需要使用以下代码实际更改设置:

    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” '。

有关设置选项的信息,请访问此处的参考:
Settings.System | Android 开发者

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

心欲静而疯不止 2024-10-07 23:48:34

您无法通过应用程序执行此操作。

您希望手机上的应用程序随意更改手机设置吗?

You can't do this from an application.

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

闻呓 2024-10-07 23:48:34

不确定它是否有帮助,但可以在无线设置中为特定接入点手动设置备用 ip、网关、dns 等。是否可以自动执行此操作或使用意图是另一个问题?

我刚刚看到这可能会有所帮助

如何使用 Android 从我的应用程序调用 Wi-Fi 设置屏幕

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

再浓的妆也掩不了殇 2024-10-07 23:48:34

android.provider.Settings.System.putString 现已弃用
这是新方法
https://developer.android.com/reference/android/net/wifi/ Wifi管理器
我也不知道如何使用这个新方法,如果有人知道请也学学我

android.provider.Settings.System.putString is deprecated now
this is the new method
https://developer.android.com/reference/android/net/wifi/WifiManager
also i don't know how to use this new method, if someone knows that please learn it to me too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文