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

Set Android IP,DNS,GATEWAY setting programmatically(以编程方式设置 Android IP、DNS、GATEWAY 设置)
本文介绍了以编程方式设置 Android IP、DNS、GATEWAY 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从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 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
iOS: Send multiple actions to a bar button(IOS:向一个栏按钮发送多个动作)