How To Forget A Wireless Network In Android Programmatically?
Solution 1:
Yes, removeNetwork()
works. I used this to remove all networks.
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
wifiManager.removeNetwork(i.networkId);
//wifiManager.saveConfiguration();
}
wifiManager.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.
https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration()
Solution 2:
The WifiManager
source code, has this method:
/*
* Delete the network in the supplicant config.
*
* This function is used instead of a sequence of removeNetwork()
* and saveConfiguration().
*
* @param config the set of variables that describe the configuration,
* contained in a {@link WifiConfiguration} object.
* @hide
*/publicvoid forgetNetwork(int netId) {
if (netId < 0) {
return;
}
mAsyncChannel.sendMessage(CMD_FORGET_NETWORK, netId);
}
But this method is @hide
, so we can't use it. But according to this comment:
This function is used instead of a sequence of
removeNetwork()
andsaveConfiguration()
You can try to use: removeNetwork()
and saveConfiguration()
instead.
Solution 3:
You can use the removeNetwork()
method to remove the redundant network connections(though I have a doubt if they will have the same netId
) and then add the connection freshly to avoid the problem you are having.
Solution 4:
wifiManager.saveConfiguration();
is deprectated in Android M. No longer need to call saveConfiguration as removeNetwork(int) already persist the configurations automatically.
https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration()
Solution 5:
By doing this it is possible to get the list of networks configured in a list, then immediately perform the deletion and save.
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
wifiManager.removeNetwork(i.networkId);
wifiManager.saveConfiguration();
}
Post a Comment for "How To Forget A Wireless Network In Android Programmatically?"