RemoveNetworkSuggestions在Android上不会断开与WiFi的连接。

huangapple go评论122阅读模式
英文:

RemoveNetworkSuggestions does not disconnect from WiFi Android

问题

我尝试在函数disconnectWiFi()内部使用removeNetworkSuggestions来断开WiFi网络连接,但设备仍然保持连接状态。我尝试过传递一个空的ArrayList,以及一个包含NetworkSuggestion的列表给removeNetworkSuggestions函数,但都没有解决这个问题。

  1. public class SingleWifi extends AppCompatActivity {
  2. private WifiManager wifiManager;
  3. private Button disconnectButton;
  4. List<WifiNetworkSuggestion> suggestionsList = new ArrayList<WifiNetworkSuggestion>();
  5. @RequiresApi(api = Build.VERSION_CODES.Q)
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_single_wifi);
  11. String wifiSSID = getIntent().getStringExtra("wifiList");
  12. connectToNetwork(wifiSSID);
  13. disconnectButton = findViewById(R.id.disconnectBtn);
  14. disconnectButton.setEnabled(false);
  15. disconnectButton.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View view) {
  18. disconnectWifi();
  19. startActivity(new Intent(SingleWifi.this, MainActivity.class));
  20. }
  21. });
  22. }
  23. @RequiresApi(api = Build.VERSION_CODES.Q)
  24. private void disconnectWifi() {
  25. if(wifiManager != null) {
  26. wifiManager.removeNetworkSuggestions(suggestionsList);
  27. Toast.makeText(this,"Disconnect successful", Toast.LENGTH_SHORT).show();
  28. }
  29. }
  30. @RequiresApi(api = Build.VERSION_CODES.Q)
  31. private void connectToNetwork(String ssid) {
  32. final WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
  33. .setSsid(ssid)
  34. .setWpa2Passphrase("password")
  35. .setIsAppInteractionRequired(true)
  36. .build();
  37. int statusCode = wifiManager.removeNetworkSuggestions(suggestionsList);
  38. suggestionsList.add(suggestion);
  39. final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  40. int status = wifiManager.addNetworkSuggestions(suggestionsList);
  41. if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
  42. Toast.makeText(this, "Connection success", Toast.LENGTH_LONG).show();
  43. }
  44. else if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
  45. Toast.makeText(this, "Already connected, update needed", Toast.LENGTH_LONG).show();
  46. status = wifiManager.removeNetworkSuggestions(suggestionsList);
  47. status = wifiManager.addNetworkSuggestions(suggestionsList);
  48. }
  49. final IntentFilter intentFilter = new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);
  50. final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  51. @Override public void onReceive(Context context, Intent intent) {
  52. if (!intent.getAction().equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
  53. return;
  54. }
  55. // Post connection
  56. disconnectButton.setEnabled(true);
  57. }
  58. };
  59. getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
  60. }
  61. }

removeNetworkSuggestions返回值为0,因此看起来输出是正确的,但实际上似乎没有真正断开与互联网的连接。

英文:

I'm trying to disconnect from WiFi network inside the function disconnectWiFi() using removeNetworkSuggestions but the device still stays connected to the network. I tried passing a null ArrayList as well as a list that contains the NetworkSuggestion to the RemoveNetworkSuggestions function and neither of it fixed the problem.

  1. public class SingleWifi extends AppCompatActivity {
  2. private WifiManager wifiManager;
  3. private Button disconnectButton;
  4. List&lt;WifiNetworkSuggestion&gt; suggestionsList = new ArrayList&lt;WifiNetworkSuggestion&gt;();
  5. @RequiresApi(api = Build.VERSION_CODES.Q)
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_single_wifi);
  11. String wifiSSID = getIntent().getStringExtra(&quot;wifiList&quot;);
  12. connectToNetwork(wifiSSID);
  13. disconnectButton = findViewById(R.id.disconnectBtn);
  14. disconnectButton.setEnabled(false);
  15. disconnectButton.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View view) {
  18. disconnectWifi();
  19. startActivity(new Intent(SingleWifi.this, MainActivity.class));
  20. }
  21. });
  22. }
  23. @RequiresApi(api = Build.VERSION_CODES.Q)
  24. private void disconnectWifi() {
  25. if(wifiManager != null) {
  26. wifiManager.removeNetworkSuggestions(suggestionsList);
  27. Toast.makeText(this,&quot;Disconnect successful&quot;, Toast.LENGTH_SHORT).show();
  28. }
  29. }
  30. @RequiresApi(api = Build.VERSION_CODES.Q)
  31. private void connectToNetwork(String ssid) {
  32. final WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
  33. .setSsid(ssid)
  34. .setWpa2Passphrase(&quot;password&quot;)
  35. .setIsAppInteractionRequired(true)
  36. .build();
  37. int statusCode = wifiManager.removeNetworkSuggestions(suggestionsList);
  38. suggestionsList.add(suggestion);
  39. final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  40. int status = wifiManager.addNetworkSuggestions(suggestionsList);
  41. if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
  42. Toast.makeText(this, &quot;Connection success&quot;, Toast.LENGTH_LONG).show();
  43. }
  44. else if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
  45. Toast.makeText(this, &quot;Already connected, update needed&quot;, Toast.LENGTH_LONG).show();
  46. status = wifiManager.removeNetworkSuggestions(suggestionsList);
  47. status = wifiManager.addNetworkSuggestions(suggestionsList);
  48. }
  49. final IntentFilter intentFilter = new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);
  50. final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  51. @Override public void onReceive(Context context, Intent intent) {
  52. if (!intent.getAction().equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
  53. return;
  54. }
  55. // Post connection
  56. disconnectButton.setEnabled(true);
  57. }
  58. };
  59. getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
  60. }
  61. }

removeNetworkSuggestions returns 0 so it does seem to produce the right output but does not seem to actually disconnect from the Internet.

答案1

得分: 1

我最终采取了完全不同的实现,放弃了Wifinetworksuggestions API,因为这个问题似乎是一个未解决的错误,可以在这里看到:https://issuetracker.google.com/issues/140398818,所以遗憾的是,目前还没有解决这个问题的方法。

英文:

I ended up going for a completely different implementation getting rid of the Wifinetworksuggestions API since this issue seem to be an unresolved bug as seen here: https://issuetracker.google.com/issues/140398818 So sadly, there is no fix for this issue as of now.

huangapple
  • 本文由 发表于 2020年9月16日 11:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63912748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定