我正在实现一种方法,如果连接到 wifi 网络失败,它会尝试使用警报对话框重新连接,这是我到目前为止的代码
public boolean autoConnect() {
String networkSSID = "xxxxx";
boolean connected = false;
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
wifiManager.startScan();
List<ScanResult> list = wifiManager.getScanResults();
AlertDialog alertDialog = new AlertDialog.Builder(CarNannyv3Activity.this).create();
alertDialog.setTitle("xxxxxxxx Not Available");
alertDialog.setMessage("Please make sure xxxxxx is turned on ");
alertDialog.setButton("Reconnect", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
autoConnect();
dialog.dismiss();
} });
for( ScanResult i : list ) {
if(i.SSID.equals("\"" + networkSSID + "\"")) {
//wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
return connected = true;
}else{
alertDialog.show();
}
}
return connected;
}
我得到两件事之一,它要么无限循环重新连接对话框,要么根本不显示,请告诉我我做错了什么。