I want to store some simple data – an alarm time and some booleans – in a way that survives device power off. I’ve been trying with sharedPreferences, but the data gets lost when I reboot the phone (it survives okay if the phone stays on).
I’ve read the docs and scanned this site as well as others, but I can’t find a definitive answer to whether sharedPreferences are this persistent.
I could store the values using SqlLite or a file, but I just wanted to know if I’m maybe doing something wrong before I write any unnecessary code.
Here are some code snippets:
public static final String ALARM_PREFERENCES = "AlarmPrefs";
public static final String ALARM_PREFERENCES_VIBRATE = "AlarmVibrate"; // Boolean
...
prefs = getSharedPreferences(ALARM_PREFERENCES, MODE_PRIVATE);
...
mVibrate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Editor editor = prefs.edit();
editor.putBoolean(ALARM_PREFERENCES_VIBRATE,mVibrate.isChecked());
editor.commit();
}
});
SharedPreferences are persistent across phone boots. Your code for saving the prefs looks ok too.