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

后端 未结 2 1921
面向向阳花
面向向阳花 2020-12-31 16:12

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

相关标签:
2条回答
  • 2020-12-31 16:46
    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();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-31 16:48

    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

    0 讨论(0)
提交回复
热议问题