我想将 SharedPreference 从 bool 值更改为字符串。默认值为 true 并将“启用”。这不是问题,因为它会自动设置。但是我如何检查它是否设置为 false 然后将新的 preferece 设置为“禁用”?在更新应用程序期间是否有一种方法可以迁移此首选项? 如果我不迁移它,此首选项的状态将在更新期间丢失...
最佳答案
SharedPreferences preferences = // obtain it
String pref;
try {
pref = preferences.getString("yourPref", "enabled");
} catch (ClassCastException e) {
// this means the pref is stored as a boolean
boolean boolPref = preferences.getBoolean("yourPref", true);
// store it instead as a String
pref = boolPref ? "enabled" : "disabled";
preferences.edit().remove("yourPref").putString("yourPref", pref).commit();
}
关于android - 如何在更新期间迁移 SharedPreferences?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24303988/