I have implemented the system to connect to wifi networks from my app programmatically, now I want to forget configured WIFI networks programmatically from the application.
I have implemented this into my application already and its been working fine on the Android 5.0 and lower devices (Less then API 22).
For Android 6.0 and the higher device it is not working (Higher and equal then API 23).
Please refer the following code:
val wifiManager = [email protected]!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.disableNetwork(i.networkId)
wifiManager.saveConfiguration()
}
I have also referred the following link: https://stackoverflow.com/a/33075445/9360112
As there are some changes in WIFI configurations in Android 6.0.
Please, help me if anyone has solution for this on Android 6.0 onwards.
For Android (general instruction using Google Marshmallow): Open Settings on your device, and tap on the WiFI icon to access WiFi network options. Tap and hold the WiFi network you want to delete, then select Forget Network from the menu that appears.
Go to Settings > Wi-Fi. Tap next to the Wi-Fi network that you want your device to forget. Tap Forget This Network, then tap Forget to confirm.
How to View Your Saved WiFi Networks – Android 10. After a while, you'd be surprised as to how many WiFi networks you're connected to. To see which networks you're a part of, go to Settings > Network and Internet > WiFi > Saved Networks. Before you access this option, you can see how many WiFi networks you have saved.
Tap “Wi-Fi” at the top. Tap the three-dot menu icon in the top-right corner and select “Advanced.” Now, select “Manage Networks.” You’ll see all the networks that you’ve connected to with your device. Select the one that you want to forget. Tap the “Forget” button.
Select “Network & Internet” from the Settings. Tap “Wi-Fi” at the top. Select “Saved Networks.” You’ll see all the networks that you’ve connected to with your device. Select the one that you want to forget. Tap the “Forget” button. The network will be removed from your list and you won’t auto-connect to it anymore.
Forgetting a network on a Samsung Galaxy works a little differently. Swipe down once from the top of your Samsung Galaxy device’s screen, and then tap the gear icon. Select “Connections” at the top of the Settings. Tap “Wi-Fi” at the top. Tap the three-dot menu icon in the top-right corner and select “Advanced.” Now, select “Manage Networks.”
Swipe down once from the top of your Samsung Galaxy device’s screen, and then tap the gear icon. Select “Connections” at the top of the Settings. Tap “Wi-Fi” at the top. Tap the three-dot menu icon in the top-right corner and select “Advanced.” Now, select “Manage Networks.” You’ll see all the networks that you’ve connected to with your device.
First thing is you don't need to use 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.
Secondly, what you're looking for is removeNetwork().
Your code will look like:
val wifiManager = [email protected]!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.removeNetwork(i.networkId)
}
Being said that... There are some changes in the Android M APIs for WifiManager.
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.
See Network Changes in Android M
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With