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

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m attempting to remove my wifi network programatically – however I cannot seem to get it to remove/forget the currently connected wifi connection. This should be a pretty simple task – so I’m not sure exactly what I’m doing wrong.

I’m using the following StackOverflow post as an example:

How to forget a wireless network in android programmatically?

     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);
       wifiManager.getConnectionInfo().getSSID()
       wifiManager.getConnectionInfo().getNetoworkId();
       wifiManager.removeNetwork(wifiConfig.networkId);
       wifiManager.saveConfiguration();

   }}
How to&Answers:

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

Answer:

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();
    }

}