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

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

RemoveNetworkSuggestions does not disconnect from WiFi Android

问题

我尝试在函数disconnectWiFi()内部使用removeNetworkSuggestions来断开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返回值为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.

public class SingleWifi extends AppCompatActivity {
private WifiManager wifiManager;
private Button disconnectButton;
List&lt;WifiNetworkSuggestion&gt; suggestionsList = new ArrayList&lt;WifiNetworkSuggestion&gt;();
@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(&quot;wifiList&quot;);
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,&quot;Disconnect successful&quot;, Toast.LENGTH_SHORT).show();
}
}
@RequiresApi(api = Build.VERSION_CODES.Q)
private void connectToNetwork(String ssid) {
final WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
.setSsid(ssid)
.setWpa2Passphrase(&quot;password&quot;)
.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, &quot;Connection success&quot;, Toast.LENGTH_LONG).show();
}
else if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
Toast.makeText(this, &quot;Already connected, update needed&quot;, 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 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:

确定