I need to save a simple field to configure my APP, because this, I won't use a database (it's only a field). I need to save true or false value for this field on a file, and every time a section of my app wants to check if it is true it has to check this textfile, and not to open a connection to a database.
I need to save the config forever. When I exit from my app, and for example, I shut down my Android device, when I start my device again and start my app, the config has to be saved.
Is this possible? How can I do it? I can't find any information about this.
I have problems with the first answer. This code is on my oncreate method:
static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
if (settings.getBoolean("showMeCheckBox", true))
showMeCheckBox.setChecked(true);
else
showMeCheckBox.setChecked(false);
applyButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (showMeCheckBox.isChecked()) {
configEditor.putBoolean("showMeCheckBox", true);
} else {
configEditor.putBoolean("showMeCheckBox", false);
}
}
});
This doesn't work. Always is selected... always true, like the default value. It doesn't matter if I checked or unchecked it.
i suggest not to use a textfile but the Preference Editor.
static SharedPreferences settings;
static SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
//store value
editor.putString("Preference_name_1", "1");
//get value
//eill return "0" if preference not exists, else return stored value
String val = settings.getString("Preference_name_1", "0");
Edit: you have to initialize the configEditor and after setting a value, you have to commit
editor = settings.edit();
editor.putBoolean("name",true);
editor.commit();