我创建了一个Android应用程序,显示可用网络列表,并在输入wifi密码后为我提供与特定WiFi网络的连接。但是我面临的问题是我的应用程序没有保留我给出的wifi密码保存。 我的申请代码如下:
private void finallyConnect(String networkPass, String networkSSID) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
// remember id
int netId = mainWifiObj.addNetwork(wifiConfig);
mainWifiObj.disconnect();
mainWifiObj.enableNetwork(netId, true);
mainWifiObj.reconnect();
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"\"" + networkSSID + "\"\"";
conf.preSharedKey = "\"" + networkPass + "\"";
mainWifiObj.addNetwork(conf);
}
private void connectToWifi(final String wifiSSID) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.connect);
dialog.setTitle("Connect to Network");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1);
Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
pass = (EditText) dialog.findViewById(R.id.textPassword);
textSSID.setText(wifiSSID);
// if button is clicked, connect to the network;
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String checkPassword = pass.getText().toString();
finallyConnect(checkPassword, wifiSSID);
dialog.dismiss();
}
});
dialog.show();
findMyip();