Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接
使用连接管理器类,我们可以访问 wifi 或 Internet 网络:
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// ...
}
其中 0
和 1
分别指移动和 wifi 连接
如果我的 Android 设备同时连接到这两个网络,我们可以在任何网络之间切换还是可以禁用任何网络?就像使用函数一样:
connec.getNetworkInfo(0).setState(NetworkInfo.State.DISCONNECTED);
Using the Connectivity Manager Class we can get access to either wifi or Internet Network:
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// ...
}
where 0
and 1
respectively refers to mobile and wifi connection
If my Android device is connected to both, can we switch between any of the networks or can we disable any of the networks? Like using a function:
connec.getNetworkInfo(0).setState(NetworkInfo.State.DISCONNECTED);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我知道启用或禁用 wifi:
根据要求,状态可能为
true
或false
。编辑:
您的清单文件中还需要以下权限:
I know of enabling or disabling wifi:
where status may be
true
orfalse
as per requirement.Edit:
You also need the following permissions in your manifest file:
启用 WiFi:
禁用 WiFi:
注意:
要使用 WiFi 状态进行访问,我们必须在 AndroidManifest.xml 文件中添加以下权限:
To Enable WiFi:
To Disable WiFi:
Note:
To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file:
完整的解决方案:
参考:http://amitkumar-android.blogspot.com/ p/installation-steps.html
A complete solution:
Reference: http://amitkumar-android.blogspot.com/p/installation-steps.html
在 Android Q (Android 10) 中,您无法再以编程方式启用/禁用 wifi。使用设置面板切换 WiFi 连接:
In Android Q (Android 10) you can't enable/disable wifi programmatically anymore. Use Settings Panel to toggle wifi connectivity:
要启用禁用 Wifi,请使用
WifiManager
类获取 Wifi 的系统(android 设备)服务:现在使用
WifiManager
类的对象wifi
获取 wifi 状态:最重要的是,不要忘记在 Android 清单文件中授予以下权限:
要获取在 Android 上启用/禁用 Wifi 的项目的详细信息和完整示例代码,请访问 我的网站链接。
To enable disable Wifi use the
WifiManager
class to get system(android device) services for Wifi :Now the object
wifi
of theWifiManager
class is used to get the wifi status:And most importantly do not forget to give the following permission in your Android Manifest File:
To get detailed info and full sample code of the project for enable/disable Wifi on android visit my website link.
从 Android Q 开始,此方法已被弃用
。尝试一下会很有帮助。
This method is deprecated now from now starting with Android Q.
Try This will really help.
在您的清单中添加此权限,然后使用上面的代码更改 WiFi 状态:
add this permission in your manifest and than use the above code to change WiFi state:
我无法直接访问上下文对象。
我的解决方案如下:
我还必须更改一些著作,例如。 WIFI_SERVICE 与 WifiService。
I could not access the context object directly.
My solution is as following:
Also I had to change some writings eg. WIFI_SERVICE vs. WifiService.
可以使用以下代码在 Android 10 之前的设备上启用/禁用 wifi:
wifiManager.setWifiEnabled(status);
但请注意,在 Android 10 上不再可能执行此操作,并且可能也会继续执行此操作。
https://issuetracker.google.com/issues/141011684
It is possible to enable/disable wifi on pre Android 10 devices using the following code:
wifiManager.setWifiEnabled(status);
But note that it is not possible to do this anymore on Android 10 and probably going ahead as well.
https://issuetracker.google.com/issues/141011684
Android 10(Q)以上的wifi无法启用/禁用您需要打开设置intent,
在manifest中,
Android 10 (Q) onwards wifi can not be enabled/disabled you need to open the setting intent,
In manifest,