Storing and retrieving shared preferences in android - android

I am trying to store some settings using preferences I am using this code:
SharedPreferences pref = getPreferences(MODE_WORLD_WRITEABLE);
pref.edit().putString("some settings", "lalal");
pref.edit().commit();
What i am doing wrong the file gets created but is empty

Try this code:
SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("some settings", "lalal");
editor.commit();
and get values using this code:
SharedPreferences shf = getSharedPreferences("myCustomSharedPrefs", MODE_WORLD_READABLE);
String strShPref = shf.getString("some settings", "");

You've got 2 different editors for your prefs, first one adds the string, and second one commits empty changes, because you've changed another editor.
Change this:
pref.edit().putString("some settings", "lalal");
pref.edit().commit();
into this:
pref.edit().putString("some settings", "lalal").commit();

Related

Get last login details without webservicing

need an idea to get last login details(DATE OF LAST LOGIN) on login without webservicing concept
Store Last login date into SQLLITE database when you need last login detail fetch this values from SQLLITE database.
instead of sqlite use SharedPreferences
you will have to create an object of shared preference and an editor
it will be similiar to this:
SharedPreferences sp;
SharedPreferences.Editor ed;
ed=sp.edit();
ed.putBoolean("username",username);
ed.commit(); //**DONT FORGET TO COMMIT**
sp = getSharedPreferences("MenuData", MODE_PRIVATE);
if(!sp.contains(null))
{
username=sp.getBoolean("username",defaultvalue);// you need to add an default value
if(username.equals("admin")){
//do watever u like
}
}
soloved the problem,
setting the value before even putting;
min=c.get(Calendar.MINUTE);
seconds=c.get(Calendar.SECOND);
SharedPreferences prefs1 = getSharedPreferences(
"DATE", MODE_PRIVATE);
datee = prefs1.getString("SUBDATE", null);
DATE1.setText(datee);
SharedPreferences.Editor editor = getSharedPreferences("DATE", MODE_PRIVATE).edit();
editor.putString("SUBDATE", min+" "+seconds+" ");
editor.commit();

How do I use sharedPreferences in Android?

I have an Android app which in one of the activities the user check one of the radio buttons. I want to save the user's choice and use its value in another activity.
To store
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("KEY", your_boolean).commit();
To retrieve
Boolean your_boolean = prefs.getBoolean("KEY", false);
You can use shared preference for saving the values.
For Saving value into SharedPreferences use below code
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(
"SHARED_PREF", MODE_PRIVATE).edit();
editor.putString("radio_value", value);
editor.commit();
For Retriving value from SharedPreferences use below code
SharedPreferences prefs = getApplicationContext().getSharedPreferences(
"SHARED_PREF", MODE_PRIVATE);
String storedValue = prefs.getString("radio_value","");

Storing data in SharedPreferences accessible to all activities

I want to store and retrieve data that is accessible to all activities in my app using SharedPreferences. Is that possible? Up until now I have been doing it such that the data is stored for a particular activity.
Yes. SharePreferences do exactly this.
In every activity you can this:
SharedPreferences prefs = getSharedPreferences(ApplicationConstants.PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(name, value);
editor.commit();
And then retrieve values in other activty doing this:
mPrefs.getString(name, "");
This is the documentation:
http://developer.android.com/reference/android/content/SharedPreferences.html
And this is a good example to start with:
http://myandroidsolutions.blogspot.it/2012/03/android-preferenceactivity.html
Yes, that's the whole purpose of it.
Here's how you should write to it, via Editor
final SharedPreferences shp = ctx.getSharedPreferences(ctx.getString(R.string.app_name), Context.MODE_PRIVATE);
final SharedPreferences.Editor ed = shp.edit();
ed.putString("var1", "var1");
ed.putString("var2", "var2");
And to load it:
shp.getString("var1", "defvalue");
I have a better version. As sometimes when you try to do getSharedPreferences you might get an error as it could not be found.
This is how I store values in my Android projects.
Add
SharedPreferences sharedPreferences=this.getSharedPreferences("packagename", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("username", "specify name here").apply();
Package Name can be directly copied from top of the activity ex: com.example.name.projectname
Retrieve
String username = sharedPreferences.getString("username","");
If you want to access values in all of your activities I think the better way is storing in a custom Application class and later in activities you can:
((CustomApplication)getApplication()).getStoredValue()
Shared preferences are stored in files and this file access is slower.
Is my example for create function for set and get an object data called "USER"
For set sharePreference data
public void saveUser(User usuario) {
SharedPreferences sharedPref = getSharedPreferences("A", Context.MODE_PRIVATE); // sharedpreference set named "A"
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", usuario.getNombre());
editor.putString("username", usuario.getUsername());
editor.putString("pass", usuario.getContrasena());
editor.putString("roll",usuario.getRol());
editor.commit();
}
For get sharePreference data
public Usuario getUser() {
SharedPreferences sharedPref = getSharedPreferences("A", Context.MODE_PRIVATE); // sharedpreference set named "A"
User usuario = new User();
usuario.setNombre(sharedPref.getString("name", "null"));
usuario.setUsername(sharedPref.getString("username", "null"));
usuario.setContrasena(sharedPref.getString("pass", "null"));
usuario.setRol(sharedPref.getString("roll", "null"));
return usuario;
}
Important: set name to sharePreference in this case "A"

Why does preferences.getString("key", "DEFAULT") always return "DEFAULT"?

I have user_preferences.xml in my XML directory. A PreferencesActivity uses this file to create the user preferences activity.. and that works. Whatever the user selects here persists. But I am unable to retrieve the value the user selected.
When I use...
SharedPreferences preferences = getSharedPreferences("user_preferences.xml", 0);
String mapTypeString = preferences.getString("map_type_pref_key", "DEFAULT");
... mapTypeString is always "DEFAULT".
It seems like my user_preferences.xml is not found when I instantiate my SharedPreferences object. But, the PreferencesActivity finds it, of course. So, what am I missing?
Many thanks!
change your code to:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String mapTypeString = preferences.getString("map_type_pref_key", "DEFAULT");
You have to commit the preferences after edit it.
SharedPreferences preferences = getSharedPreferences("user_preferences.xml", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("map_type_pref_key", "blah_blah");
editor.commit();

Android - How Do I Set A Preference In Code

I have an Android application in which I have my preferences in an XML file, which works fine. I now want to set one of the preferences using code instead of displaying the entire preference screen, how would I go about doing this?
I assume by preferences you are referring to your application's preferences and not Android phone settings.
To store preferences between runs of you application you need to do the following
Create a SharedPreferences object
SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE);
String n identifies your preferences and the second argument is the mode they'll be accessed
Instantiate an Editor object
SharedPreferences.Editor editor = settings.edit();
Note: do not try settings.editor.edit(), this will not make a persistent object and the code below will not work
Write your preferences to the buffer
editor.put...(String, value)
There are numerous put function, putString, putBoolean, etc. The String is the key ("version", "good run") and the value is the value ("1.5.2", true)
Flush the buffer
editor.commit();
This actually writes you put to the preferences. If your app crashes before this line then the preferences will not be written. There is also a documented bug: commit() is supposed to return a boolean indicating success or failure. Last I checked it always returned false.
These preferences will by stored on the phone and will only be accessible to your application.
More documentation is here
I tried this but didn't work:
SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE);
Try this instead:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
You can save something in the sharedpreferences by using below code
public static void save(String valueKey, String value) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = prefs.edit();
edit.putString(valueKey, value);
edit.commit();
}
To read preferences:
public static String read(String valueKey, String valueDefault) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
return prefs.getString(valueKey, valueDefault);
}

Categories

Resources