SharedPreferences 在为其创建单独的 class 后不保存

SharedPreferences Not Saving after creating separate class for it

我正在开发一个应用程序,我将值保存在 SharedPrefernces 中。我创建了一个单独的 class SharedPreferenceforAlarm 用于设置 或获取存储在 SharedPreferences 中的值。要设置或获取存储的值,我会从不同的活动中调用 class 对象。

我面临的问题是值没有正确保存。

例如(见代码):

alarmisSet 的默认值:False

警报名称的默认值:无警报

如果我从任何 activity 调用 setAlarm(this, true),我得到的日志是 "SHAREDPREFERENCES, Alarm is set: false;"

同样的情况,如果我调用 setAlarmName(),默认 alarmName 打印的不是我给的那个。

值得一提的是,我已经为第一个 运行 创建了一个 setupHelp activity应用程序。当我安装应用程序并 运行ning 时,setuphelp activity 是 运行ning。第二次,setuphelp 没有显示。这意味着 setFirstRun()getFirstRun()的 class SharedPreferencesforAlarm 正常工作,但之后就不行了。

还值得一提的是,在为 alarmisSet 设置默认值时将 false alarmName 无警报 ,Logcat 显示正确显示输出。

PS: 我检查过不同的 PC,但都没有成功。

/*****************************Shared Preference Class**************************************//


public class SharedPreferencesforAlarm {

public static final String TAG = "SHAREDPREFERENCES" ;
public static final String MyPREFERENCES = "Alarm" ;
public static final String firstRun="Firstrun";
public static final String alarmisSet="alarmisSet";
public static final String alarmName="alarmName";



public SharedPreferencesforAlarm() {
    super();
}

public void  setFirstRun(Context context) {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    Editor editor=mySettings.edit();
    editor.putBoolean(firstRun, true);
    editor.commit();
}

public Boolean getFirstRun(Context context) {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    return(mySettings.getBoolean(firstRun, false));
}

public void setAlarm(Context context, Boolean value) {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    Editor editor=mySettings.edit();
    editor.putBoolean(alarmisSet, value);
    editor.commit();
    Log.d(TAG,"Alarm is Set:"+value);
}

public Boolean getAlarm(Context context, Boolean value) {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    return(mySettings.getBoolean(alarmisSet, value));

}


public void setAlarmName(Context context,String name) {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    editor = mySettings.edit();
    editor.putString(alarmName, name);
    editor.commit();
    Log.d(TAG, "Alarm Name:" + name);
}

public String getAlarmName() {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    return(mySettings.getString(alarmName, null));

}

}

    /***********CODE FOR CALLING THE SHARED PREFERENCE CLASS FROM ANY ACTVITY*****************************//
SharedPreferencesforAlarm sharedPreferences = new    SharedPreferencesforAlarm(); //creating the object
sharedPreferences.setAlarm(this, true);
sharedPreferences.setAlarmName(this, alarmName.getText().toString()); //alarmName is a TextView

首先,使您的 class 抽象化,并将其方法静态化,如下所示:

public abstract class SharedPreferencesforAlarm {

    public static final String TAG = "SHAREDPREFERENCES";
    public static final String MyPREFERENCES = "Alarm";
    public static final String alarmisSet = "alarmisSet";
    public static final String alarmName = "alarmName";

    public static void setAlarm(Context context, Boolean value) {
        SharedPreferences mySettings;
        mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        Editor editor = mySettings.edit();
        editor.putBoolean(alarmisSet, value);
        editor.commit();
        Log.d(TAG, "Alarm is Set:" + value);
    }

    // repreat for other classes
}

然后,您可以像这样调用您的方法,而无需一直创建新的 SharedPrefs 对象:

SharedPreferencesforAlarm.setAlarm(this, true);
SharedPreferencesforAlarm.setAlarmName(this, alarmName.getText().toString());

我在我的项目中就是这样做的,这是我目前能看到的唯一区别。

  You write your code like this you have problem with Editor class.



public void setAlarm(Context context, boolean value) {
    SharedPreferences mySettings;
    mySettings = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = mySettings.edit();
    editor.putBoolean(alarmisSet, value);
    editor.commit();
    Log.d(TAG,"Alarm is Set:"+value);
}       

我遇到了问题:关于 sharedpreferences.getBoolean() 方法的错误假设。

我假设

getBoolean(parameter, true)

return 如果参数为真且第二个参数为真则为真,如果参数值为假且第二个参数为真则为假但实际上它 return 仅是参数值。