如何读/写布尔首选项

时间:2012-04-29 04:25:42

标签: android preferences sharedpreferences

编辑:此代码实际上有效。我在使用它的代码中遇到了问题。无论如何都要离开它以防任何人发现它有用。

我有一个类有两个方法来编写和读取一个布尔持久化的首选项。但是,如果我写一个新值然后尝试读取它,我仍然会得到旧值。只有当我杀死应用程序并重新启动它时,我才会获得新值。知道问题是什么吗?

 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);
}

3 个答案:

答案 0 :(得分:4)

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()

答案 1 :(得分:0)

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()

答案 2 :(得分:0)

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");