编辑:这段代码实际上有效。我在使用它的代码中遇到了问题。无论如何都要保留它,以防有人会发现它有用。
我有一个类,其中包含两种方法来编写和读取布尔持久首选项。但是,如果我写入一个新值,然后尝试读取它,我仍然会得到旧值。只有当我杀死应用程序并重新启动它时,我才会获得新值。知道问题是什么吗?
Context mContext;
....
public void writeFlag(boolean flag) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext);
Editor editor = sharedPreferences.edit();
editor.putBoolean("mykey", flag);
editor.commit();
}
public boolean readFlag() {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(mContext);
return sharedPreferences.getBoolean("mykey", false);
}
public static boolean getBooleanFromSP(String key) {
// TODO Auto-generated method stub
SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}//getPWDFromSP()
public static void saveBooleanInSP(String key, boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}//savePWDInSP()
public static boolean getBooleanFromSP(String key) {
// TODO Auto-generated method stub
SharedPreferences preferences =
getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ",
android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}//getPWDFromSP()
public static void saveBooleanInSP(String key, boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}//savePWDInSP()
public class SharePref {
private static final String TAG = SharePref.class.getSimpleName();
private static SharePref mThis;
private Context mContext;
private SharedPreferences mPreference;
private SharePref() {
}
public static void init(Context context) {
if (mThis == null) {
mThis = new SharePref();
mThis.setData(context);
}
}
private void setData(Context context) {
mThis.mContext = context;
mPreference = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
}
public static SharePref getInstance() {
return mThis;
}
public void writeString(String tag, String data) {
SharedPreferences.Editor editor = mPreference.edit();
editor.putString(tag, data).apply();
}
public String readString(String tag) {
if (mPreference == null)
mPreference = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
return mPreference.getString(tag, null);
}
public void writeBoolean(String tag, boolean data) {
SharedPreferences.Editor editor = mPreference.edit();
editor.putBoolean(tag, data).apply();
}
public boolean readBoolean(String tag) {
return mPreference.getBoolean(tag, false);
}
public void clear(String tag) {
mPreference.edit().remove(tag).apply();
}
}
//在活动中
SharePref.init(this);
SharePref.getInstance().writeString("key","value");