在我的应用程序中,用户可以保存程序将连接到的服务器配置文件。由于我不知道用户将添加的连接配置文件的数量,我不能简单地将其放入SettingsActivity
.
由于我不想再打扰 XML 文件,我想我可以像这样保存 SharedPreferences:
server1hostname
server1username
server1password
server2hostname
server2username
server2password
server3hostname
server3username
server3password
第一个问题:如果我想将所有内容保存在标准首选项文件中(与在某处创建自己的文件相反),是否有更好的方法?
第二个问题:我已经或多或少地实现了这一点。奇怪的是:它适用于现有设置(最初是通过 a 创建的SettingsActivity
),但不适用于新条目。
这是源代码(Powerline
是服务器配置文件的类名):
@Override
public void onBackPressed() {
super.onBackPressed();
// Save powerline item
Powerline newPowerline = new Powerline();
newPowerline.setAddress(etAddress.getText().toString());
newPowerline.setSendPort(Integer.parseInt(etSendPort.getText().toString()));
newPowerline.setReceivePort(Integer.parseInt(etReceivePort.getText().toString()));
newPowerline.setUsername(etUsername.getText().toString());
newPowerline.setPassword(etPassword.getText().toString());
int powerlinePositionInSettings = getIntent().getIntExtra("powerlinePositionInSettings", -1);
if (option.equals("create")) {
if (newPowerline.create(this, powerlinePositionInSettings))
finish();
} else {
if (powerlinePositionInSettings >= 0)
if (newPowerline.change(this, powerlinePositionInSettings))
finish();
}
}
这是Powerline
该类的创建函数:
public boolean create(Context context, int powerlinePositionInSettings) {
String positionString = String.valueOf(powerlinePositionInSettings);
Log.i("Powerline", "Saving new powerline to settings to position " + positionString + ".");
try {
SharedPreferences.Editor prefsEditor = PreferenceManager.getDefaultSharedPreferences(context).edit();
prefsEditor.putString("powerline" + positionString + "Address", this.getAddress());
prefsEditor.putString("powerline" + positionString + "SendPort", String.valueOf(this.getSendPort()));
prefsEditor.putString("powerline" + positionString + "ReceivePort", String.valueOf(this.getReceivePort()));
prefsEditor.putString("powerline" + positionString + "Username", this.getUsername());
prefsEditor.putString("powerline" + positionString + "Password", getPassword());
Log.i("Prefs", "Saving \"" + "powerline" + positionString + "Address" + "\" to be \"" + this.getAddress() + "\"");
return prefsEditor.commit();
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
这是Powerline
该类的更改功能:
public boolean change(Context context, int powerlinePositionInSettings) {
String positionString = String.valueOf(powerlinePositionInSettings);
Log.i("Powerline", "Saving powerline to settings to position " + positionString + ".");
try {
SharedPreferences.Editor prefsEditor = PreferenceManager.getDefaultSharedPreferences(context).edit();
prefsEditor.putString("powerline" + positionString + "Address", this.getAddress());
prefsEditor.putString("powerline" + positionString + "SendPort", String.valueOf(this.getSendPort()));
prefsEditor.putString("powerline" + positionString + "ReceivePort", String.valueOf(this.getReceivePort()));
prefsEditor.putString("powerline" + positionString + "Username", this.getUsername());
prefsEditor.putString("powerline" + positionString + "Password", getPassword());
Log.i("Prefs", "Saving \"" + "powerline" + positionString + "Address" + "\" to be \"" + this.getAddress() + "\"");
return prefsEditor.commit();
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
更改功能存储的值确实被保存了。create 函数保存的新值不是。
有人有想法吗?我没有看到这两个功能有任何显着差异。