Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Android shared preferences value in activity/normal class

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.

like image 654
Sandeep Avatar asked Sep 21 '11 08:09

Sandeep


People also ask

How do you retrieve a value from SharedPreferences?

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.

How do we get access to the preference via getPreference () method?

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);


3 Answers

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); 
like image 132
Umesh Avatar answered Oct 01 '22 22:10

Umesh


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.

like image 38
Dave Cox Avatar answered Oct 01 '22 22:10

Dave Cox


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
    }
like image 36
samreen Avatar answered Oct 02 '22 00:10

samreen