Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences Long value

Here I'm creating a SharedPreferences if I'm not wrong i'm using this code :

SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putLong("ExactTime", minutesToMilliseconds(NumberPicker2.getValue()));
editor.commit();

What I'm doing is I get a value from a NumberPicker and I want to save this value and once an user restart the APP or even reboot the device (I don't know if SharedPreferences does it...) when the user opens the APP it still the same number picked from NumberPicker.

And I want to use this in a class that extends a Service. What I've tried is :

SharedPreferences sp = this.getSharedPreferences("ExactTime", Context.MODE_PRIVATE);
WifiTimeSearch = sp.getLong("ExactTime", 0);

Here is where I'm lost... I've read that this "0" means a default value but I want to use the same value that I've stored on SharedPreferences... And when I try to use this Long value, the value is "0".

What I'm doing wrong?

like image 535
Skizo-ozᴉʞS Avatar asked Jun 29 '15 19:06

Skizo-ozᴉʞS


People also ask

Can we store large amount of data in SharedPreferences?

SharedPreferences are not intended to store a lot of data, there is no limit per se (since it is an xml file), but for larger sets of data, I would suggest using Room (or SQLite for the older projects). There is also another reason why storing in a database makes more sense.

How can I get SharedPreferences value?

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. getInt(String key, int defValue): This method is used to retrieve an int value from the preferences.

What is the maximum number of shared preferences you can create?

there is no limit in Shared Preference.


3 Answers

You're reading and writing to different SharedPreference files.

Also, by using getPreferences(), you're writing to SharedPreferences that are local to that Activity.

From the source code:

/**
     * Retrieve a {@link SharedPreferences} object for accessing preferences
     * that are private to this activity.  This simply calls the underlying
     * {@link #getSharedPreferences(String, int)} method by passing in this activity's
     * class name as the preferences name.
     *
     * @param mode Operating mode.  Use {@link #MODE_PRIVATE} for the default
     *             operation, {@link #MODE_WORLD_READABLE} and
     *             {@link #MODE_WORLD_WRITEABLE} to control permissions.
     *
     * @return Returns the single SharedPreferences instance that can be used
     *         to retrieve and modify the preference values.
     */
    public SharedPreferences getPreferences(int mode) {
        return getSharedPreferences(getLocalClassName(), mode);
    }

To make it work, just use the default SharedPreferences for your app:

Writing:

    SharedPreferences sp =  PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sp.edit();
    editor.putLong("ExactTime", minutesToMilliseconds(NumberPicker2.getValue()));
    editor.commit();

Reading:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    WifiTimeSearch = sp.getLong("ExactTime", 0);

Note that as long as you are in an Activity or a Service, you can use this as the Context.

like image 160
Daniel Nugent Avatar answered Oct 21 '22 07:10

Daniel Nugent


In SharedPreferences, The first parameter is the key and the second parameter is the MODE. So to COMMIT as well as to retrieve value we have to use same key.

try this code to commit :

SharedPreferences sp = getSharedPreferences("ExactTime",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putLong("ExactTime", minutesToMilliseconds(NumberPicker2.getValue()));
editor.commit();
like image 34
Anand Singh Avatar answered Oct 21 '22 06:10

Anand Singh


I guess problem is you are passing preference file name as "ExactTime" while reading from preferences, while saving you are not specifying any

Try this

SharedPreferences sp = getSharedPreferences(null,Context.MODE_PRIVATE);
WifiTimeSearch = sp.getLong("ExactTime", 0);
like image 26
Akhil Avatar answered Oct 21 '22 05:10

Akhil