I have a preferences activity with a checkbox "Enable Service".
I read the value like this :
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(con);
ServiceEnabled_Pref = prefs.getBoolean("EnableService", true);
ok, but how can I set this Preference ?
this is not a CustomShared Preference, It is a DefaultShared Preferences and it seems that there is not a method putBoolean for DefaultSharedPreferences.
I need this, cause I have a Widget with a button that needs to set this value to true/false
programatically:
prefs.editor().putBoolean("EnableService", true).commit();
however your best bet for defining the preferenceActivity and its defaults is via an xml file as decribed here with the android:defaultValue attribute
Use the shared preferences editor:
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html
Related
Here is code I am Using to create and store value in Preference.
outgoing is String variable.
SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("PhoneNo","Hi");
editor.commit();
Here is the code to get value from SharedPreference.
SharedPreferences sp
=getSharedPreferences(outgoing,Activity.MODE_PRIVATE);
String calln = sp.getString("PhoneNo","0");
Toast.makeText(mContext, "SHARED"+calln,Toast.LENGTH_LONG).show();
You should probably call the getSharedPreferences on the context from which you are accessing them.
Source
Therefore, depending on how you can access your context, if you pass it to some other activity or in an asynchronous task, here are some examples of usage:
this.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
context.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
Also, one way you can test your stuff is to use a listener when SharedPreferences get changed:
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
Called when a shared preference is changed, added, or removed.
here is how to do that
You can also use the Preference Manager to obtain the SharedPreferences:
PreferenceManager.getSharedPreferences(YOUR_CONTEXT).getString(
"PhoneNo", "0");
Or to store them:
PreferenceManager.getSharedPreferences(YOUR_CONTEXT).edit().putString(
"PhoneNo", "Hi").commit();
The most likely reason that Shared Preference always returns the default value is that you saved the value in one preference file and then tried to retrieve it in another preference file. This can happen if you do call getPreferences() from different Activities, because getPreferences() creates a different preference file based on the activity it is created in.
Solution
The easiest solution is to always get your shared preferences like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
This will use a single preference file for the entire app.
Alternate solution
If for some reason you need to use different preference files, then you can do
final static String PREF_FILE_1 = "pref_file_1";
...
SharedPreferences sharedPref = context.getSharedPreferences(PREF_FILE_1, Context.MODE_PRIVATE);
Just make sure you always use the right file name for the preferences you are trying to save and retrieve.
Local preferences
If you really only need a preference for a specific Activity, then you can use getPreferences(Context.MODE_PRIVATE). Just don't expect to be able to retrieve the values from another Activity in the same way.
See also
This answer describes the differences between the various ways of obtaining SharedPreferences.
Difference between getDefaultSharedPreferences and getSharedPreferences
Mess with the shared preferences of android - which function to use?
change this Activity.MODE_PRIVATE to this Activity.MODE_MULTI_PROCESS, issue is probably due to different context during storing value and accessing value.
When putting values, try changing this:
SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
to this:
SharedPreferences sp = getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
Same when getting values - don't forget to add getApplicationContext() in your call to SharedPreferences
EDIT:
Check that your "outgoing" String is the exact same in both Activities
Here are my questions:
How can I get the value of a switch preference?
Are things inside a preference fragment already a shared preference?
If so, how can I access shared preferences from a preference fragment?
1. To get the value of a switch:
CompoundButton cb = (CompoundButton)view.findViewById(R.id.myswitch);
if(cb.isChecked())
cb.setChecked(false);
else
cb.setChecked(true);
Reference: Toggle Buttons
2. Yes, the elements in a PreferenceFragment are automatically stored.
3. To get the SharedPreference object of a PreferenceFragment, use:
getDefaultSharedPreferences(Context context)
This is an amazing example on how to make a PreferenceFragment:
http://android-er.blogspot.hu/2012/07/example-of-using-preferencefragment.html
Basically the way it works is that you define the preferences with their types and keys. The preference fragment stores its data in the SharedPreferences, which you obtain via Android getDefaultSharedPreferences , and edit its values and obtain them from an Editor.
In a Preference Fragment, you automatically have the data of the prefs linked between the Fragment and the SharedPrefs.
Also look at this example code: http://www.mysamplecode.com/2011/11/android-shared-preferences-example_12.html
I've implemented a OnPreferenceChangeListener on two preference object in my preference page ( extends PreferenceActivity)
But ever since the preference value isn't updated upon change,
I even tried using :
SharedPreferences sharedPreferences = getSharedPreferences("myCustomSharedPrefs",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(preference.getKey(), Integer.parseInt(newValue.toString()));
editor.commit();
Where preference is the changed preference and newValue is the new value...
What am I missing ?
Thanks
You need to return true to get the value updated, see the Android documentation.
Can you see if preference.getKey() is giving the correct name? Can you print in your console and see. Code looks clean to me. May be the name is different than what is expected.
Also make sure that the value is also having the correct value.
If I have a onSharedPreferenceChanged event in my PreferenceActivity, that is checking if a CheckBoxPreference is checked or not and setting setEnabled on some other Preference, does the actual value of the other Preference get changed in the SharedPreferences, or do I have to manually set them?
What I have is:
public void onSharedPreferenceChanged(Settings sharedPreferences, String key)
{
CheckBoxPreference cbUpdatesEnabled = (CheckBoxPreference)getPreferenceScreen().findPreference("updatesenabled");
CheckBoxPreference cbVibrate = (CheckBoxPreference)getPreferenceScreen().findPreference("vibrate");
cbVibrate.setEnabled(cbUpdatesEnabled.isChecked());
}
Which toggles whether the "vibrate" checkbox is enabled or disabled in the UI, but then do I also have to use:
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("vibrate", cbUpdatesEnabled.isChecked());
editor.commit();
Or is there some other way of updating the SharedPreferences? It doesn't look like the value is automatically saved just based on if it's enabled or not.
You don't have to manually update the preference value if you are using PreferenceActivity.
For more information check following tutorials.
Click here
Click here
Currently, I'm using the following code across all of my activities in my app to store application level variables and carry values between activities..
prefs = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
I didn't have a PreferenceActivity prior to this, but now I do and I am looking to store a few user prefs from this new PreferenceActivity in the same sharedPreferences tag, "MyPrefs".
I know I can access the PreferenceActivity SharedPrefs from my activities via
prefs = PreferenceManager.getDefaultSharedPreferences(this);
but I would like those values saved to my current sharedPreferences tag, "MyPrefs", but I'm not sure how to do this.
Thanks in advance..
You can change the default name of SharedPreferences file used by PreferenceActivity.
I did this in onCreate method of PreferenceActivity by adding the following piece of code:
getPreferenceManager().setSharedPreferencesName("MyPrefs");
Yes, you can http://idlesun.wordpress.com/2011/04/08/how-to-make-preferenceactivity-use-non-default-sharedpreferences/
You can't. the PreferneceManager always uses this.getPackageName () + "_preferences as preference name. Sorry to break the bad news. This is also important when you wish to use the new backup framework.
You can of course replace MyPrefs with this.getPackageName () + "_preferences.