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?
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.
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.
there is no limit in Shared Preference.
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.
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();
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);
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