How to forget wifi network programatically in android targeted sdk 30

226 Views Asked by At

I want to forget the wifi connected network from the device.I gone through most of the code but they are not working.

 public void disconnectWifi(){
        Log.i(TAG,"In disconnectWifi()");
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        wifiManager.forget(wifiInfo.getNetworkId(), new WifiManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.i(TAG,"wifi is disconnected");
            }

           @Override
            public void onFailure(int reason) {
                Log.i(TAG,"wifi is failed disconnected");
            }
        });
    }

Here on Success is never called.

public void disconnectCurrentNetwork(){
        WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if(wifiManager != null && wifiManager.isWifiEnabled()){
            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            Log.d(TAG,"lIST"+list);
            int netId = wifiManager.getConnectionInfo().getNetworkId();
            wifiManager.removeNetwork(netId);
        }

    }

Here netid shows -1 value and it also not forget the wifi network. I have read that it should not work you must use have to provide permission WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN.I also mention that in manifest as below but no changes happpens.I have read that Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps. Since we can forget network by going to the settings of mobile.So there should be some method to forget the WiFi network

  <uses-permission android:name="WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN"/>
0

There are 0 best solutions below