Android - 仍然无法以编程方式删除 Wifi 网络

Android - Still can't remove Wifi Network Programatically

阅读其他几篇 questions/answers 后,我仍然无法使用 wifiManager.removeNetwork 工作。

根据:

Android - Cant Remove Wifi Network Programatically- The method removeNetwork(int) in the type WifiManager is not applicable for the arguments (String)

How to forget a wireless network in android programmatically?

...我的代码应该可以工作:

WifiManager wifiMAN = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

for (WifiConfiguration wifiCon : wifiMAN.getConfiguredNetworks()) {
    if (wifiCon.SSID.equals( "\""+ targetNetworkName +"\"")) {
        wifiMAN.removeNetwork(wifiCon.networkId)) {
        wifiMAN.saveConfiguration();
        break;
    }
}

但是,当我在 API 23 上测试时,它不起作用。查看代码,没有任何东西出现贬值,并且代码可以在 API 21 上运行。有人有任何想法吗?

这是有意为之的行为。自 Android 起,不允许 M 个应用修改非其创建的网络。

参见:https://code.google.com/p/android/issues/detail?id=187347 and https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network