public final class Preferences
- Object
- 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
String myToken = "abc123";
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).
Methods
Inherited methods
Method details
getPreferencesLocation
public static String 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
public static void setPreferencesLocation(String storageFileName)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
storageFileNameString- the name of the preferences file
set
public static void set(Map<String, Object> values)Sets a set of preference values as a batch, and performs a single save.
Parameters
valuesMap<String, Object>- The key/value pairs to set in preferences.
set
public static void set(String pref, String s)Sets a preference value
Parameters
prefString- the key any unique none null value that doesn’t start with cn1
sString- a String
set
public static void set(String pref, int i)Sets a preference value
Parameters
prefString- the key any unique none null value that doesn’t start with cn1
iint- a number
set
public static void set(String pref, long l)Sets a preference value
Parameters
prefString- the key any unique none null value that doesn’t start with cn1
llong- a number
set
public static void set(String pref, double d)Sets a preference value
Parameters
prefString- the key any unique none null value that doesn’t start with cn1
ddouble- a number
set
public static void set(String pref, float f)Sets a preference value
Parameters
prefString- the key any unique none null value that doesn’t start with cn1
ffloat- a number
delete
public static void delete(String pref)Deletes a value for the given setting
Parameters
prefString- the preference value
clearAll
public static void clearAll()Remove all preferences
set
public static void set(String pref, boolean b)Sets a preference value
Parameters
prefString- the key any unique none null value that doesn’t start with cn1
bboolean- the value
get
public static String get(String pref, String def)Gets the value as a String
Parameters
prefString- the preference key
defString- the default value
Returns
the default value or the value
getAndSet
public static String getAndSet(String pref, String def)Gets the value as a String if the value is null def is returned and saved
Parameters
prefString- the preference key
defString- the default value
Returns
the default value or the value
getAndSet
public static int getAndSet(String pref, int def)Gets the value as a number if the value is null def is returned and saved
Parameters
prefString- the preference key
defint- the default value
Returns
the default value or the value
get
public static int get(String pref, int def)Gets the value as a number
Parameters
prefString- the preference key
defint- the default value
Returns
the default value or the value
getAndSet
public static long getAndSet(String pref, long def)Gets the value as a number if the value is null def is returned and saved
Parameters
prefString- the preference key
deflong- the default value
Returns
the default value or the value
get
public static long get(String pref, long def)Gets the value as a number
Parameters
prefString- the preference key
deflong- the default value
Returns
the default value or the value
getAndSet
public static double getAndSet(String pref, double def)Gets the value as a number if the value is null def is returned and saved
Parameters
prefString- the preference key
defdouble- the default value
Returns
the default value or the value
get
public static double get(String pref, double def)Gets the value as a number
Parameters
prefString- the preference key
defdouble- the default value
Returns
the default value or the value
getAndSet
public static float getAndSet(String pref, float def)Gets the value as a number if the value is null def is returned and saved
Parameters
prefString- the preference key
deffloat- the default value
Returns
the default value or the value
get
public static float get(String pref, float def)Gets the value as a number
Parameters
prefString- the preference key
deffloat- the default value
Returns
the default value or the value
getAndSet
public static boolean getAndSet(String pref, boolean def)Gets the value as a number if the value is null def is returned and saved
Parameters
prefString- the preference key
defboolean- the default value
Returns
the default value or the value
get
public static boolean get(String pref, boolean def)Gets the value as a number
Parameters
prefString- the preference key
defboolean- the default value
Returns
the default value or the value
addPreferenceListener
public static void addPreferenceListener(String pref, PreferenceListener listener)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
prefString- The preference to listen to
listenerPreferenceListener- The listener to add, which cannot be null.
removePreferenceListener
public static boolean removePreferenceListener(String pref, PreferenceListener listener)Remove the listener for the specified preference.
Parameters
prefString- The preference that the listener listens to
listenerPreferenceListener- The listener to remove
Returns
true if the listener was removed, false if it was not found.