-1
if (Build.VERSION.SDK_INT >= 23) { 

      wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); 
      wifi.startScan(); 

      ///////////////////// 
      beforeOpenDoorWifi = wifi.getConnectionInfo().getNetworkId(); 
      wifi.setWifiEnabled(true); 
      // setup a wifi configuration 
      WifiConfiguration wc = new WifiConfiguration(); 

      List lst = wifi.getConfiguredNetworks(); 


      wc.SSID = "\"" + sSid + "\""; 
      wc.preSharedKey = "\"" + pass + "\""; 

      wc.status = WifiConfiguration.Status.DISABLED; 
      wc.priority = 40; 
      wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
      wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
      wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 

      // connect to and enable the connection 

      int netId = wifi.addNetwork(wc); 
      if (-1 == netId) { 
       // enableWifi(wifiName); 

       List<WifiConfiguration> llst = wifi.getConfiguredNetworks(); 
       Iterator<WifiConfiguration> it = llst.iterator(); 
       while (it.hasNext()) { 
        WifiConfiguration wconfig = it.next(); 
        if (wconfig.SSID.equals("\"" + sSid + "\"")) { 
         wifi.enableNetwork(wconfig.networkId, true); 

         setFalseOther(mm); 
         notifyDataSetChanged(); 
         mSession.setPrefrenceString("networkName", sSid); 
         Intent i = new Intent(mContext, Connect.class); 
         i.putExtra("networkName", mm.getName()); 
         i.putExtra("networkId", mm.getId()); 
         i.putExtra("AdminID", mm.getUserId()); 
         i.putExtra("networkConnection", "true"); 

         mContext.startActivity(i); 

         break; 
        } 
       } 

此代碼工作正常,連接到給定的SSID和密碼,但我想斷開或刪除該網絡的詳細信息。「disableNetwork(networkId)」不給任何幫助。如果任何人可以給連接提供更好的解決方案,斷開或刪除棉花糖的WiFi網絡將有所幫助。提前致謝。刪除或斷開Android棉花糖特定的WiFi網絡?

回答

0
 public void forget(final String s) { 
    final WifiConfiguration wifiConfigure = this.getWifiConfigure(s); 
    if (wifiConfigure != null) { 
     final Method[] declaredMethods = this.mWifiManager.getClass().getDeclaredMethods(); 
     Boolean b = false; 
     for (final Method method : declaredMethods) { 
      if (method.getName().equals("forget")) { 
       b = true; 
       try { 

        method.invoke(this.mWifiManager, wifiConfigure.networkId, null); 
       } 
       catch (Exception ex) { 
        b = false; 
       } 
       break; 
      } 
     } 
     if (!b) { 
      this.disableAndRemoveWifi(s); 
     } 
    } 
}