wifimanager 设置页面同时设置保存其他参数的完整代码
时间: 2023-10-01 11:11:35 浏览: 76
WiFiManager-master.zip
以下是一个示例代码,可以在设置WIFI的同时保存其他参数:
```
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
EditText wifiNameEditText = (EditText) findViewById(R.id.wifi_name_edit_text);
EditText wifiPasswordEditText = (EditText) findViewById(R.id.wifi_password_edit_text);
// 设置WIFI
wifiManager.setWifiEnabled(true);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + wifiNameEditText.getText().toString() + "\"";
wifiConfiguration.preSharedKey = "\"" + wifiPasswordEditText.getText().toString() + "\"";
int networkId = wifiManager.addNetwork(wifiConfiguration);
wifiManager.enableNetwork(networkId, true);
// 保存其他参数
SharedPreferences sharedPreferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", "John Doe");
editor.putInt("age", 30);
editor.apply();
```
其中,`wifiNameEditText`和`wifiPasswordEditText`是用于输入WIFI名称和密码的EditText控件。保存其他参数使用了SharedPreferences进行处理,将键值对存储在名为"my_prefs"的SharedPreferences中。注意,最后需要调用`editor.apply()`方法来提交保存的数据。
阅读全文