SharedPreferences Long-term value

Here I am creating SharedPreferences

, if I am not mistaken I am using this code:

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

      

What I do I get value

from NumberPicker

and I want to keep this one value

and after restarting the APP user or even restarting the device (I don't know if SharedPreferences

does this ...) when the user opens the APP, it is still the same number selected from NumberPicker

.

And I want to use this in a class that extendsService

. I tried:

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

      

This is where I got lost ... I read that this "0" means the default, but I want to use the same value that I saved on SharedPreferences

... And when I try to use that value the Long

value is "0".

What am I doing wrong?

+3


source to share


3 answers


You are reading and writing different SharedPreference files.

Also, using getPreferences()

, you write to SharedPreferences which are local to this action.

From 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 standard SharedPreferences for your application:



Record:

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

      

Please note that as long as you are in an action or service, you can use it this

as context.

+3


source


In SharedPreferences, the first parameter is the key and the second parameter is MODE. Therefore, for COMMIT

as well as for the value, retrieve

we must use the same key.

try this code:



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

      

+2


source


I think the problem is that you are passing the preference filename as " ExactTime " when reading from settings, and when you save you are not specifying any

try it

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

      

+1


source







All Articles