在为

时间:2015-06-25 07:52:50

标签: android android-activity sharedpreferences

开发一个应用程序,我可以在SharedPrefernces中保存值。我已经为设置创建了一个单独的类 SharedPreferenceforAlarm 或获取存储在SharedPreferences中的值。要设置或获取存储的值,iam从不同的活动中调用类对象。

我遇到的问题是值没有正确保存。

例如(见代码):

alarmisSet的默认值:False

alarmName的默认值:无警报

如果iam从任何活动中调用setAlarm(this,true),则记录我得到的内容 “SHAREDPREFERENCES,闹钟已设置:false;”

同样的情况如果我打电话 setAlarmName() ,默认 alarmName 正在打印而不是我给的那个。

值得一提的是,我为应用的首次运行创建了 setupHelp 活动。当我安装应用程序并运行它时, setuphelp 活动正在运行。第二次, setuphelp 未显示。这意味着类 SharedPreferencesforAlarm <的 setFirstRun() getFirstRun() em> 工作正常,但之后没有。

还值得一提的是,将 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

3 个答案:

答案 0 :(得分:0)

首先,让你的类抽象化,其方法是静态的,如下所示:

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

我在假设

getBoolean(parameter, true)

如果参数为true且第二个参数为true,则返回true;如果参数值为false且第二个参数为true,则返回false,但实际上仅返回参数值。