是否可以取消正在进行的WifiManager连接请求?

时间:2018-08-01 13:48:06

标签: android android-wifi

在我的应用中,我使用以下代码连接到特定的主机位:

 private void connectToNetwork(final WifiNetwork network){

    NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
    final WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + network.getNetworkName() + "\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.addNetwork(conf);

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
                if (i.SSID != null && i.SSID.equals(conf.SSID)) {

                    Log.e("connectToNetwork", "Disconnecting from " + wifiManager.getConnectionInfo().getSSID());
                    boolean isDisconnected = wifiManager.disconnect();
                    Log.v("connectToNetwork", "isDisconnected: " + isDisconnected);

                    boolean isEnabled = wifiManager.enableNetwork(i.networkId, true);
                    Log.v("connectToNetwork", "isEnabled: " + isEnabled);

                    Log.e("connectToNetwork", "Trying to reconnect to " + conf.SSID);
                    break;
                }
            }
}

我想设置连接的时间限制,比如说10秒,所以我使用了TimerTask来检查是否建立了连接。我的问题是,我可以取消已经在进行的连接请求吗?

0 个答案:

没有答案