英文:
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<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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论