public class Preferences extends Object
Simple map like class to store application and Codename One preference
settings in the Storage
.
Simple usage of the class for storing a String
token:
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)
.
Modifier and Type | Method and Description |
---|---|
static void |
addPreferenceListener(String pref,
PreferenceListener listener)
Adds a preference listener for the specified property to the list of listeners.
|
static void |
clearAll()
Remove all preferences
|
static void |
delete(String pref)
Deletes a value for the given setting
|
static boolean |
get(String pref,
boolean def)
Gets the value as a number
|
static double |
get(String pref,
double def)
Gets the value as a number
|
static float |
get(String pref,
float def)
Gets the value as a number
|
static int |
get(String pref,
int def)
Gets the value as a number
|
static long |
get(String pref,
long def)
Gets the value as a number
|
static String |
get(String pref,
String def)
Gets the value as a String
|
static boolean |
getAndSet(String pref,
boolean def)
Gets the value as a number if the value is null def is returned and saved
|
static double |
getAndSet(String pref,
double def)
Gets the value as a number if the value is null def is returned and saved
|
static float |
getAndSet(String pref,
float def)
Gets the value as a number if the value is null def is returned and saved
|
static int |
getAndSet(String pref,
int def)
Gets the value as a number if the value is null def is returned and saved
|
static long |
getAndSet(String pref,
long def)
Gets the value as a number if the value is null def is returned and saved
|
static String |
getAndSet(String pref,
String def)
Gets the value as a String if the value is null def is returned and saved
|
static String |
getPreferencesLocation()
Returns the location within the storage of the preferences file to an arbitrary name.
|
static boolean |
removePreferenceListener(String pref,
PreferenceListener listener)
Remove the listener for the specified preference.
|
static void |
set(Map<String,Object> values)
Sets a set of preference values as a batch, and performs a single save.
|
static void |
set(String pref,
boolean b)
Sets a preference value
|
static void |
set(String pref,
double d)
Sets a preference value
|
static void |
set(String pref,
float f)
Sets a preference value
|
static void |
set(String pref,
int i)
Sets a preference value
|
static void |
set(String pref,
long l)
Sets a preference value
|
static void |
set(String pref,
String s)
Sets a preference value
|
static void |
setPreferencesLocation(String storageFileName)
Sets the location within the storage of the preferences file to an arbitrary name.
|
public static void setPreferencesLocation(String storageFileName)
storageFileName
- the name of the preferences filepublic static String getPreferencesLocation()
public static void set(Map<String,Object> values)
values
- The key/value pairs to set in preferences.public static void set(String pref, String s)
pref
- the key any unique none null value that doesn't start with cn1s
- a Stringpublic static void set(String pref, int i)
pref
- the key any unique none null value that doesn't start with cn1i
- a numberpublic static void set(String pref, long l)
pref
- the key any unique none null value that doesn't start with cn1l
- a numberpublic static void set(String pref, double d)
pref
- the key any unique none null value that doesn't start with cn1d
- a numberpublic static void set(String pref, float f)
pref
- the key any unique none null value that doesn't start with cn1f
- a numberpublic static void delete(String pref)
pref
- the preference valuepublic static void clearAll()
public static void set(String pref, boolean b)
pref
- the key any unique none null value that doesn't start with cn1b
- the valuepublic static String get(String pref, String def)
pref
- the preference keydef
- the default valuepublic static String getAndSet(String pref, String def)
pref
- the preference keydef
- the default valuepublic static int getAndSet(String pref, int def)
pref
- the preference keydef
- the default valuepublic static int get(String pref, int def)
pref
- the preference keydef
- the default valuepublic static long getAndSet(String pref, long def)
pref
- the preference keydef
- the default valuepublic static long get(String pref, long def)
pref
- the preference keydef
- the default valuepublic static double getAndSet(String pref, double def)
pref
- the preference keydef
- the default valuepublic static double get(String pref, double def)
pref
- the preference keydef
- the default valuepublic static float getAndSet(String pref, float def)
pref
- the preference keydef
- the default valuepublic static float get(String pref, float def)
pref
- the preference keydef
- the default valuepublic static boolean getAndSet(String pref, boolean def)
pref
- the preference keydef
- the default valuepublic static boolean get(String pref, boolean def)
pref
- the preference keydef
- the default valuepublic static void addPreferenceListener(String pref, PreferenceListener listener)
pref
- The preference to listen tolistener
- The listener to add, which cannot be null.public static boolean removePreferenceListener(String pref, PreferenceListener listener)
pref
- The preference that the listener listens tolistener
- The listener to remove