Android - Cant Remove Wifi Network Programatically- The method removeNetwork(int) in the type WifiManager is not applicable for the arguments (String)
removeNetwork()
takes only integer parameters. The networkSSID
is a string value. That's the cause for the error. I see that you are using SSID which is a string. You have to give the network id which is integer. You can try getConnectionInfo().getSSID()
and compare with your ssid, if they are same then you can try getting getConnectionInfo().getNetoworkId()
which should give the connected network's network id, which you can use to removeNetwork.
Try this:
public class KillTimer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.killtimer);
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.getConnectionInfo().getNetworkId();
wifiManager.removeNetwork(networkId);
wifiManager.saveConfiguration();
}}
Latest Updates As Of 10 June 2019
There are some changes for Wifi Manager in Android 6.0 onwards.
Any Wi-Fi configuration created by an active Device Owner can no longer be modified or deleted by the user if WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN is non-zero.
Active Device Owners have the privilege of editing or removing any Wi-Fi configurations, including those not created by them.
For more details, please refer: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
private void RemoveWifiNetworks() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
//int networkId = wifiManager.getConnectionInfo().getNetworkId();
wifiManager.removeNetwork(i.networkId);
wifiManager.saveConfiguration();
}
}