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

时间:2016-12-22 12:39:11

标签: java android android-studio wifimanager

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

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

例如:

  1. 我已连接到wifi网络:networkA
  2. 我打开了应用
  3. wifimanager连接到wifi网络:networkB
  4. 我关闭了应用
  5. wifimanager断开网络连接并将其从配置中删除
  6. 在此之后,我希望我的手机能够以最佳信号自动连接到已配置的网络。 (在这种情况下是networkA)

    当我关闭应用程序时,它会断开网络连接但是如果我打开手机上的wifi网络选项卡,它会将“已禁用”视为网络A的状态。

    如何再次启用networkA?

    我曾尝试wifiManager.enableNetwork(networkBid, false),但在打开应用后会断开网络连接,然后重新连接到networkA。

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

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

在连接到networkB之前,我获得了当前的网络ID(networkA)。

关闭应用后,它会重新连接到networkA。

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