需要在 Android 6.0 中以编程方式忘记配置的 Wifi 网络
Need to forget configured Wifi network programmatically in Android 6.0
我已经实现了以编程方式从我的应用程序连接到 wifi 网络的系统,现在我想忘记从应用程序以编程方式配置的 WIFI 网络。
我已经在我的应用程序中实现了它并且它在 Android 5.0 和更低版本的设备上运行良好(小于 API 22)。
对于 Android 6.0 和更高版本的设备它不工作(更高和等于 API 23)。
请参考以下代码:
val wifiManager = this@SelectWifiSettingsActivity.baseContext!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.disableNetwork(i.networkId)
wifiManager.saveConfiguration()
}
我还提到了以下 link:
由于 Android 6.0.
中的 WIFI 配置发生了一些变化。
如果有人在 Android 6.0 之后有解决方案,请帮助我。
首先你不需要使用 saveConfiguration().
This method was deprecated in API level 26.
There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically.
其次,您要找的是removeNetwork()。
您的代码将如下所示:
val wifiManager = this@SelectWifiSettingsActivity.baseContext!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.removeNetwork(i.networkId)
}
据说... WifiManager 的 Android M API 有一些变化。
Your apps can now change the state of WifiConfiguration objects only
if you created these objects. You are not permitted to modify or
delete WifiConfiguration objects created by the user or by other apps.
我已经实现了以编程方式从我的应用程序连接到 wifi 网络的系统,现在我想忘记从应用程序以编程方式配置的 WIFI 网络。
我已经在我的应用程序中实现了它并且它在 Android 5.0 和更低版本的设备上运行良好(小于 API 22)。
对于 Android 6.0 和更高版本的设备它不工作(更高和等于 API 23)。
请参考以下代码:
val wifiManager = this@SelectWifiSettingsActivity.baseContext!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.disableNetwork(i.networkId)
wifiManager.saveConfiguration()
}
我还提到了以下 link:
由于 Android 6.0.
中的 WIFI 配置发生了一些变化。如果有人在 Android 6.0 之后有解决方案,请帮助我。
首先你不需要使用 saveConfiguration().
This method was deprecated in API level 26.
There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically.
其次,您要找的是removeNetwork()。
您的代码将如下所示:
val wifiManager = this@SelectWifiSettingsActivity.baseContext!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.removeNetwork(i.networkId)
}
据说... WifiManager 的 Android M API 有一些变化。
Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.