Class Preferences
Simple map like class to store application and Codename One preference
settings in the com.codename1.io.Storage.
Simple usage of the class for storing a String token:
// save a token to storage
Preferences.set("token", myToken);
// get the token from storage or null if it isn't there
String token = Preferences.get("token", null);
Notice that this class might get somewhat confusing with primitive numbers e.g. if you use
Preferences.set("primitiveLongValue", myLongNumber) then invoke
Preferences.get("primitiveLongValue", 0) you might get an exception!
This would happen because the value is physically a Long object but you are trying to get an
Integer.
The workaround is to remain consistent and use code like this Preferences.get("primitiveLongValue", (long)0).
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddPreferenceListener(String pref, PreferenceListener listener) Adds a preference listener for the specified property to the list of listeners.static voidclearAll()Remove all preferencesstatic voidDeletes a value for the given settingstatic booleanGets the value as a numberstatic doubleGets the value as a numberstatic floatGets the value as a numberstatic intGets the value as a numberstatic longGets the value as a numberstatic StringGets the value as a Stringstatic booleanGets the value as a number if the value is null def is returned and savedstatic doubleGets the value as a number if the value is null def is returned and savedstatic floatGets the value as a number if the value is null def is returned and savedstatic intGets the value as a number if the value is null def is returned and savedstatic longGets the value as a number if the value is null def is returned and savedstatic StringGets the value as a String if the value is null def is returned and savedstatic StringReturns the location within the storage of the preferences file to an arbitrary name.static booleanremovePreferenceListener(String pref, PreferenceListener listener) Remove the listener for the specified preference.static voidSets a preference valuestatic voidSets a preference valuestatic voidSets a preference valuestatic voidSets a preference valuestatic voidSets a preference valuestatic voidSets a preference valuestatic voidSets a set of preference values as a batch, and performs a single save.static voidsetPreferencesLocation(String storageFileName) Sets the location within the storage of the preferences file to an arbitrary name.
-
Method Details
-
getPreferencesLocation
Returns the location within the storage of the preferences file to an arbitrary name. This is useful in a case of encryption where we would want preferences to use a different file name.
Returns
the storage file name
-
setPreferencesLocation
Sets the location within the storage of the preferences file to an arbitrary name. This is useful in a case of encryption where we would want preferences to use a different file name.
Parameters
storageFileName: the name of the preferences file
-
set
-
set
-
set
Sets a preference value
Parameters
-
pref: the key any unique none null value that doesn't start with cn1 -
i: a number
-
-
set
Sets a preference value
Parameters
-
pref: the key any unique none null value that doesn't start with cn1 -
l: a number
-
-
set
Sets a preference value
Parameters
-
pref: the key any unique none null value that doesn't start with cn1 -
d: a number
-
-
set
Sets a preference value
Parameters
-
pref: the key any unique none null value that doesn't start with cn1 -
f: a number
-
-
delete
Deletes a value for the given setting
Parameters
pref: the preference value
-
clearAll
public static void clearAll()Remove all preferences -
set
Sets a preference value
Parameters
-
pref: the key any unique none null value that doesn't start with cn1 -
b: the value
-
-
get
-
getAndSet
-
getAndSet
Gets the value as a number if the value is null def is returned and saved
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
get
Gets the value as a number
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
getAndSet
Gets the value as a number if the value is null def is returned and saved
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
get
Gets the value as a number
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
getAndSet
Gets the value as a number if the value is null def is returned and saved
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
get
Gets the value as a number
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
getAndSet
Gets the value as a number if the value is null def is returned and saved
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
get
Gets the value as a number
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
getAndSet
Gets the value as a number if the value is null def is returned and saved
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
get
Gets the value as a number
Parameters
-
pref: the preference key -
def: the default value
Returns
the default value or the value
-
-
addPreferenceListener
Adds a preference listener for the specified property to the list of listeners. When calling this method, it is advisable to also read the current value and set it, since the value may have changed since the last time the listener was removed. (Should this return the current value of the preference?)
Parameters
-
pref: The preference to listen to -
listener: The listener to add, which cannot be null.
-
-
removePreferenceListener
Remove the listener for the specified preference.
Parameters
-
pref: The preference that the listener listens to -
listener: The listener to remove
Returns
true if the listener was removed, false if it was not found.
-
-