I am saving a value in Preference, but it always give me the default value. When the app is opened, I can get the actual value. But when I am getting the value from the IntentService, it always give me the default value.
Code for saving the value:
prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
Code for reading the value:
prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
String value = prefs.getString(key, defaultValue);
But if i change file name then its working for some period but afterward again it start giving default value
Try changing your access mode from Context.MODE_PRIVATE since you are reading it from outside.
Try
prefs = context.getSharedPreferences(NAME, Context.MODE_MULTI_PROCESS);
Retrieving stuff from Shared Preferences:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
String s = sp.getString("key", null); // get "value" from the Shared Preferences
Related
i am using shared preference in android but it return null,
i saw a lot of code example for this and i can't see any error in my code
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Log.v("sharedpref",""+username+": "+password);
editor.putString("email", username);
editor.putString("pass",password);
editor.apply();
and here i am retrieving the data from shared preference (in another activity)
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Log.v("shersend",""+sp.getString("email", "Empty")+": "+sp.getString("pass", "Empty"));
String email = sp.getString("email", "Empty");
String pass = sp.getString("pass", "Empty");
so is there any problem with my code?
and is there a better to write this?
You are saving the preference in one Activity say A, And accessing in another activity say B, and context of both activities are different and so preferences values cannot be accessed as mode is private.
try
this.getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Please try that way, Change this code block
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
to this code block
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Use getApplicationContext()
SharedPreferences sp =getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Log.v("sharedpref",""+username+": "+password);
editor.putString("email", username);
editor.putString("pass",password);
editor.commit();
I usually use this code for creating sharedPrefs file:
SharedPrefrences SP = getSharedPrefrences("MYPREF",0);
SP.getString("Username","x");
But now if I see this directory:
"/data/data/packageName/"
There is no shared_prefs directory.
Another thing : if I use Editor its work properly.
First you need to store some data in shared preferences using apply()/commit() method so that you retrieve data from there.
SharedPrefrences SP = getSharedPrefrences("MYPREF",0);
SP.edit().putString("Username","SOME NAME").apply();
System.out.println("Username is : "+SP.getString("Username","x"));
First of all you have to SET the data with an Editor
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Pepito");
editor.apply();
Then you can read it as follows :
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "Here's the default value");
If you've set the value after make that getString() your String value will be "Pepito", otherwise you'll get "Here's the default value"
I am using following code:
SharedPreferences sharedPref = getSharedPreferences(GlobalDefines.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putBoolean(GlobalDefines.USER_IS_LOGGED_IN, false);
editor.remove(GlobalDefines.USER_NAME);
editor.remove(GlobalDefines.USER_PASSWORD);
editor.commit();
test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
The string "test" has a value when I get the value from the shared preferences for the first time; when I remove another value from the preferences and want to get the same value (GCM_KEY) again, it is returned empty.
Why is that?
editor.clear() tells the editor that you want to remove ALL values from your SharedPreferences. Remove this line and you will see the expected behavior.
I am trying to use a value stored in a shared preference to help style a listview and when I use this code it comes back the default value;
SharedPreferences pref = context.getSharedPreferences("Level", 0);
mCounter = pref.getInt("Level", 3);
This is the code I used to store the preference:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
pref.edit().putInt("Level", 1).commit();
getSharedPreferences(String name, int mode)
where name is the name of the preferences file
getInt (String key, int defValue)
where key is the actual preference key
Do you have named your preferences and the key both "Level"? If not that is the problem.
This should work:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
mCounter = pref.getInt("Level", 3);
Are you sure the preferences have been changed from their default settings? When using the SharedPreferences.Editor you must remember to call commit() to save your changes.
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
mCounter = pref.getInt("Level", 3);
You have to do above code..
Use below code to store & get value from prefrance. this is best way to do this.
For store data:
pref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editPref = pref.edit();
editPref.putBoolean("logedin", true);
editPref.commit();
For Get Data:
pref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editPref.getBoolean("logedin", false);
You need to set same shared preference name when you want to get the value.Like here you want to get your value in "Level" preference but put in different shared preference " com.komodostudios.asllessons".
So that you get the default value for "Level"
Code will be like that:
To store:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
Editor editPreference=pref .edit();
editPreference.putInt("Level",2);
editPreference.commit();
To retrieve:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
int mCounter = pref.getInt("Level",1);
I am using Android 2.1 sdk and I am trying to save user loggin session in to Shared preferences, the thing is after saving the value to the shared preference I am unable to retrive it. Here I am pasting the code I used to save and fetch value from SharedPrefrence.
public void setValue(String name, String value, String prefName) {
sharedPref = mContext.getSharedPreferences(prefName, Context.MODE_PRIVATE);
sharedPref.edit().putString(name, value);
sharedPref.edit().commit();
}
public String getValue(String name, String prefName) {
String value = null;
sharedPref = mContext.getSharedPreferences(prefName, Context.MODE_PRIVATE);
value = sharedPref.getString(name, value);
return value;
}
Did i miss some thing in this code, I am not retrieving any exceptions while saving and retrieving the value. Thanks for any help.
Every call to edit() returns you a new Editor instance. So you get an instance, make a change and leave it alone. Then you get a second one and commit that without changes, which results in no value changes in the preferences.
Rather chain in the commit():
sharedPref.edit().putString(name, value).commit();
Alternatively break it up into multiple lines with one specific instance:
Editor e = sharedPref.edit();
e.putString(name, value);
e.commit();
private SharedPreferences myPrefs;
myPrefs = Actionactivity.this.getSharedPreferences("myPrefs", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Mobile_no", getText_no.getText().toString().trim());
prefsEditor.commit();
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
myPrefs.getString("Mobile_no", "");
try this one code work