I have made a shared preference activity that store the user settings, now i want to get values in a activity or normal java class.please provide a solution or example i have already tried this code but failed.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences channel=this.getSharedPreferences(strFile, Context.MODE_PRIVATE);
strChannel=channel.getString(keyChannel,"Default").toString();
Toast.makeText(getApplicationContext(), strChannel, Toast.LENGTH_LONG).show();
}
in this code strfile
for eg. com.android.pack.ClassName
is SharedPreference Activity
from values to be fetched, and keyChannel is key that is same in SharedPreference Activity
.
Kindly provide the solution.
getAll(): This method is used to retrieve all values from the preferences. getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value from the preferences. getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences.
The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name. SharedPreferences preferences = getPreferences(MODE_PRIVATE); int storedPreference = preferences. getInt("storedInt", 0);
If you have a SharedPreferenceActivity by which you have saved your values
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String imgSett = prefs.getString(keyChannel, "");
if the value is saved in a SharedPreference in an Activity then this is the correct way to saving it.
SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); SharedPreferences.Editor editor = shared.edit(); editor.putString(keyChannel, email); editor.commit();// commit is important here.
and this is how you can retrieve the values.
SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); String channel = (shared.getString(keyChannel, ""));
Also be aware that you can do so in a non-Activity class too but the only condition is that you need to pass the context of the Activity. use this context in to get the SharedPreferences.
mContext.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
I tried this code, to retrieve shared preferences from an activity, and could not get it to work:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); sharedPreferences.getAll(); Log.d("AddNewRecord", "getAll: " + sharedPreferences.getAll()); Log.d("AddNewRecord", "Size: " + sharedPreferences.getAll().size());
Every time I tried, my preferences returned 0, even though I have 14 preferences saved by the preference activity. I finally found the answer. I added this to the preferences in the onCreate section.
getPreferenceManager().setSharedPreferencesName("defaultPreferences");
After I added this statement, my saved preferences returned as expected. I hope that this helps someone else who may experience the same issue that I did.
This is the procedure that seems simplest to me:
SharedPreferences sp = getSharedPreferences("MySharedPrefs", MODE_PRIVATE);
SharedPreferences.Editor e = sp.edit();
if (sp.getString("sharedString", null).equals("true")
|| sp.getString("sharedString", null) == null) {
e.putString("sharedString", "false").commit();
// Do something
} else {
// Do something else
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With