我正在尝试做一个检查器,我想将一个值保存到 SharedPreferences 中。但我不确定它是否有效
我为保存值所做的事情是:*
SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
boolean firstrun = prefs.getBoolean("firstrun", true);
db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory
if (firstrun) {
SharedPreferences.Editor editor = prefs.edit();
db.execSQL("CREATE TABLE startValue (ID Integer Primary Key, myValue Integer)");
db.execSQL("INSERT INTO startValue (myValue) VALUES (2)");
editor.putBoolean("firstrun", false);
editor.apply();
}
// Save the state
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
从另一个活动中清除偏好是:
try{
db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory
db.execSQL("DROP TABLE IF EXISTS startValue");
db.close();
SharedPreferences preferences = getPreferences(0);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("firstrun");
editor.clear();
editor.commit();
this.finish();
}
catch(SQLException ex)
{
//catch error here
}
问题
但是当我正在测试时,我看到它并没有清除偏好。我做错了什么还是?