从配置的网络中删除网络并启用所有其他网络

Remove network from configured networks and enable all other networks

我正在 android 工作室制作一个应用程序,它使用 wifimanager 连接到特定的 wifi 网络。

当我关闭应用程序时,我希望它从配置中删除 wifi 网络并启用所有其他 wifi 网络。

例如:

  1. 我连接到 wifi 网络:networkA
  2. 我打开应用程序
  3. wifimanager 连接到 wifi 网络:networkB
  4. 我关闭应用程序
  5. wifimanager 断开网络 B 并将其从配置中删除

在此之后,我希望我的 phone 自动连接到具有最佳信号的已配置网络。 (在本例中为网络 A)

当我关闭应用程序时,它会断开网络 B,但如果我在 phone 上打开 wifi 网络选项卡,它会显示 "Disabled" 作为网络 A 的状态。

如何重新启用网络 A?

我试过 wifiManager.enableNetwork(networkBid, false) 但它只会在打开应用程序时断开网络 A,然后重新连接到网络 A。

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myWebView = (WebView) findViewById(R.id.activity_main);
    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration conf = new WifiConfiguration();

    int WDTWifiCount = 0;
    List<ScanResult> results = wifiManager.getScanResults();
    for (ScanResult scanResult : results) {
        if (scanResult.SSID.contains("WDT_")) {
            SSIDList.add(WDTWifiCount, scanResult.SSID);
            WDTWifiCount++;
        }
    }

    if (WDTWifiCount < 1) {
        startActivityForResult(new Intent(this, NoWifi.class), 2);
    } else if (WDTWifiCount > 1) {
        Intent intent = new Intent(this, MultipleWifi.class);
        intent.putStringArrayListExtra("SSIDList", (ArrayList<String>) SSIDList);
        startActivityForResult(intent, 1);
    } else if (WDTWifiCount == 1) {
        networkSSID = SSIDList.get(0);
        if (networkSSID.length() == 8) {
            networkPass = "24816";
        } else if (networkSSID.length() == 9) {
            networkPass = "248163264";
        }
        conf.SSID = "\"" + networkSSID + "\"";
        conf.preSharedKey = "\"" + networkPass + "\"";

        wifiManager.addNetwork(conf);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();

                break;
            }
        }
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        myWebView.setWebChromeClient(new WebChromeClient());
        if (networkSSID.length() == 9) {
            myWebView.loadUrl("http://192.168.1.1/index.html");
        } else if (networkSSID.length() == 8) {
            myWebView.loadUrl("http://192.168.1.3/home.htm");
        }
    }
}  @override public void onBackPressed() {
    String webUrl = myWebView.getUrl();
    if (webUrl.equals("http://192.168.1.1/index.html") || webUrl.equals("http://192.168.1.3/home.htm") || webUrl == null) {
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                networkID = i.networkId;
                break;
            }
        }
        wifiManager.removeNetwork(networkID);
        super.onBackPressed();
    } else {
        myWebView.goBack();
    }
}

我找到了解决方案:

我在连接到网络 B 之前获取了当前网络 ID (网络 A)。

关闭应用程序后,它重新连接到网络A。

我宁愿它不禁用所有网络,以便智能手机可以连接到任何网络,但这是一个不错的选择。