RemoveNetworkSuggestions 不会断开与 WiFi 的连接 Android

RemoveNetworkSuggestions does not disconnect from WiFi Android

我正在尝试使用 removeNetworkSuggestions 在函数 disconnectWiFi() 中断开与 WiFi 网络的连接,但设备仍保持与网络的连接。我尝试将空 ArrayList 以及包含 NetworkSuggestion 的列表传递给 RemoveNetworkSuggestions 函数,但都没有解决问题。

public class SingleWifi extends AppCompatActivity {
private WifiManager wifiManager;
private Button disconnectButton;
List<WifiNetworkSuggestion> suggestionsList = new ArrayList<WifiNetworkSuggestion>();

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_wifi);
    String wifiSSID = getIntent().getStringExtra("wifiList");
    connectToNetwork(wifiSSID);
    disconnectButton = findViewById(R.id.disconnectBtn);
    disconnectButton.setEnabled(false);
    disconnectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            disconnectWifi();
            startActivity(new Intent(SingleWifi.this, MainActivity.class));
        }
    });
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void disconnectWifi() {
    if(wifiManager != null) {
        wifiManager.removeNetworkSuggestions(suggestionsList);
        Toast.makeText(this,"Disconnect successful", Toast.LENGTH_SHORT).show();
    }
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void connectToNetwork(String ssid) {
    final WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase("password")
            .setIsAppInteractionRequired(true)
            .build();
    int statusCode = wifiManager.removeNetworkSuggestions(suggestionsList);
    suggestionsList.add(suggestion);
    final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    int status = wifiManager.addNetworkSuggestions(suggestionsList);

    if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
        Toast.makeText(this, "Connection success", Toast.LENGTH_LONG).show();
    }
    else if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
        Toast.makeText(this, "Already connected, update needed", Toast.LENGTH_LONG).show();
        status = wifiManager.removeNetworkSuggestions(suggestionsList);
        status = wifiManager.addNetworkSuggestions(suggestionsList);
    }

    final IntentFilter intentFilter = new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);

    final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override public void onReceive(Context context, Intent intent) {
            if (!intent.getAction().equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
                return;
            }
            // Post connection
            disconnectButton.setEnabled(true);
        }
    };
    getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
}
}

removeNetworkSuggestions returns 0 所以它似乎产生了正确的输出,但似乎并没有真正断开与 Internet 的连接。

我最终选择了一个完全不同的实现来摆脱 Wifinetworksuggestions API 因为这个问题似乎是一个未解决的错误,如下所示:https://issuetracker.google.com/issues/140398818 很遗憾,没有修复这个问题截至目前。