的Android - 广东话删除WLAN网络Programatically-在类型WifiMana

2019-08-31 08:25发布

我试图以编程方式删除我的wifi网络 - 但我似乎无法得到它删除/忘记当前连接的WiFi连接。 这应该是一个非常简单的任务 - 所以我不知道到底是什么,我做错了。

我使用下面的StackOverflow职位为例:

如何忘记在android系统的无线网络编程?

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

   }}

Answer 1:

removeNetwork()只需要整数参数。 该networkSSID是一个字符串值。 这是该错误的原因。 我看到你正在使用的SSID是一个字符串。 你必须给网络ID是整数。 您可以尝试getConnectionInfo().getSSID()和你的SSID进行比较,如果它们是相同的,那么你可以尝试让getConnectionInfo().getNetoworkId()这应该给所连接的网络的网络ID,你可以用它来removeNetwork。

试试这个:

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


Answer 2:

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

}


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