9

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

   }}
Community
  • 1
  • 1
Bill Florentine
  • 81
  • 1
  • 5
  • 13

2 Answers2

10

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

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
jaga
  • 753
  • 11
  • 18
  • I actually don't need to hard code a specific SSID to be removed - I simply want to remove whichever SSID is currently connected. I updated my source code above (I omitted the section you mentioned creating to compare them - since I want to simply disconnect whichever is connected) Can you take a look and see if my current implementation looks correct to you? – Bill Florentine Apr 17 '13 at 07:04
  • [I really appreciate your help with this!] – Bill Florentine Apr 17 '13 at 07:10
  • You have got most of it right. Try the code in the edited response – jaga Apr 17 '13 at 07:10
  • You are a gentleman and a scholar... Thank you sir! (It's working!) – Bill Florentine Apr 17 '13 at 07:17
4
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();
    }

}
josliber
  • 43,891
  • 12
  • 98
  • 133