public final class Preferences

  1. Object
  2. 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

public static String getPreferencesLocation()Returns the location within the storage of the preferences file to an arbitrary name.
public static void setPreferencesLocation(String storageFileName)Sets the location within the storage of the preferences file to an arbitrary name.
public static void set(Map<String, Object> values)Sets a set of preference values as a batch, and performs a single save.
public static void set(String pref, String s)Sets a preference value
public static void set(String pref, int i)Sets a preference value
public static void set(String pref, long l)Sets a preference value
public static void set(String pref, double d)Sets a preference value
public static void set(String pref, float f)Sets a preference value
public static void delete(String pref)Deletes a value for the given setting
public static void clearAll()Remove all preferences
public static void set(String pref, boolean b)Sets a preference value
public static String get(String pref, String def)Gets the value as a String
public static String getAndSet(String pref, String def)Gets the value as a String if the value is null def is returned and saved
public static int getAndSet(String pref, int def)Gets the value as a number if the value is null def is returned and saved
public static int get(String pref, int def)Gets the value as a number
public static long getAndSet(String pref, long def)Gets the value as a number if the value is null def is returned and saved
public static long get(String pref, long def)Gets the value as a number
public static double getAndSet(String pref, double def)Gets the value as a number if the value is null def is returned and saved
public static double get(String pref, double def)Gets the value as a number
public static float getAndSet(String pref, float def)Gets the value as a number if the value is null def is returned and saved
public static float get(String pref, float def)Gets the value as a number
public static boolean getAndSet(String pref, boolean def)Gets the value as a number if the value is null def is returned and saved
public static boolean get(String pref, boolean def)Gets the value as a number
public static void addPreferenceListener(String pref, PreferenceListener listener)Adds a preference listener for the specified property to the list of listeners.
public static boolean removePreferenceListener(String pref, PreferenceListener listener)Remove the listener for the specified preference.

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

storageFileName String
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

values Map<String, Object>
The key/value pairs to set in preferences.

set

public static void set(String pref, String s)
Sets a preference value

Parameters

pref String
the key any unique none null value that doesn’t start with cn1
s String
a String

set

public static void set(String pref, int i)
Sets a preference value

Parameters

pref String
the key any unique none null value that doesn’t start with cn1
i int
a number

set

public static void set(String pref, long l)
Sets a preference value

Parameters

pref String
the key any unique none null value that doesn’t start with cn1
l long
a number

set

public static void set(String pref, double d)
Sets a preference value

Parameters

pref String
the key any unique none null value that doesn’t start with cn1
d double
a number

set

public static void set(String pref, float f)
Sets a preference value

Parameters

pref String
the key any unique none null value that doesn’t start with cn1
f float
a number

delete

public static void delete(String pref)
Deletes a value for the given setting

Parameters

pref String
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

pref String
the key any unique none null value that doesn’t start with cn1
b boolean
the value

get

public static String get(String pref, String def)
Gets the value as a String

Parameters

pref String
the preference key
def String
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

pref String
the preference key
def String
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

pref String
the preference key
def int
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

pref String
the preference key
def int
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

pref String
the preference key
def long
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

pref String
the preference key
def long
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

pref String
the preference key
def double
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

pref String
the preference key
def double
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

pref String
the preference key
def float
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

pref String
the preference key
def float
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

pref String
the preference key
def boolean
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

pref String
the preference key
def boolean
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

pref String
The preference to listen to
listener PreferenceListener
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

pref String
The preference that the listener listens to
listener PreferenceListener
The listener to remove

Returns

true if the listener was removed, false if it was not found.