6

编辑:此代码实际上有效。我在使用它的代码中有问题。无论如何都要留下它,以防有人发现它有用。

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

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

3 回答 3

5
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()
于 2012-04-29T04:27:09.083 回答
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()
于 2017-07-28T10:44:06.227 回答
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");                                                                                    
于 2017-07-28T10:55:28.670 回答