java.lang.Object | |
↳ | com.neomades.preference.Preferences |
Object used for modifying application preferences.
Changes are not directly modified to the persistent storage until the
save()
method is called.
To get a Preferences, call Preferences.getPreferences(String)
with the name of preferences.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
cancel()
Cancels (rollback)
Preferences changes. | ||||||||||
boolean |
contains(String key)
Checks whether the key exists into this
Preferences . | ||||||||||
boolean |
exists()
Returns if this Preferences object exists into device application storage.
| ||||||||||
boolean |
getBoolean(String key, boolean defaultValue)
Returns the preference values if it exist, or the default value.
| ||||||||||
boolean |
getBoolean(String key)
Retrieves a boolean value from the preferences.
| ||||||||||
byte[] |
getByteArray(String key)
Retrieves a byte[] value from the preferences.
| ||||||||||
byte[] |
getByteArray(String key, byte[] defaultValue)
Retrieves a byte[] value from the preferences.
| ||||||||||
int |
getInt(String key, int defaultValue)
Returns the preference values if it exist, or the default value.
| ||||||||||
int |
getInt(String key)
Retrieves an integer value from the preferences.
| ||||||||||
long |
getLong(String key)
Retrieves an long value from the preferences.
| ||||||||||
long |
getLong(String key, long defaultValue)
Returns the preference values if it exist, or the default value.
| ||||||||||
static Preferences |
getPreferences(String name)
Retrieve and holds the content of the preferences with the given
name . | ||||||||||
String |
getString(String key)
Retrieves an
String value from the preferences. | ||||||||||
String |
getString(String key, String defaultValue)
Returns the preference values if it exist, or the default value.
| ||||||||||
boolean |
hasChanges()
Returns if this Preferences object contains changes from the persistent
store.
| ||||||||||
void | put(String key, String value) | ||||||||||
void | put(String key, boolean value) | ||||||||||
void | put(String key, byte[] value) | ||||||||||
void | put(String key, int value) | ||||||||||
void | put(String key, long value) | ||||||||||
void |
remove(String key)
Deletes from preferences the key entry and its value.
| ||||||||||
static void |
removePreferences(String name)
Removes a preference from the application preferences list.
| ||||||||||
synchronized void |
save()
Saves (Commit)
Preferences changes. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Cancels (rollback) Preferences
changes.
All modified data (set with put
methods) will be cancelled.
After calling this method, hasChanges()
will return false.
Checks whether the key exists into this Preferences
.
key | the name of the preference entry |
---|
Returns if this Preferences object exists into device application storage.
If the Preferences
object should exist, then call
save()
method.
Preferences
exists.Returns the preference values if it exist, or the default value.
The contains(String)
method could check if the preference exists.
key | Name of the preference. |
---|---|
defaultValue | Value to return if this preference does not exist. |
defaultValue
Retrieves a boolean value from the preferences.
Returns the preference values if it exist, or the default value. The
contains(String)
method could check if the preference exists.
The call preferences.getBoolean("key")
is equivalent to :
preferences.getBoolean("key", false)
key | Name of the preference. |
---|
Retrieves a byte[] value from the preferences.
key | Name of the preference. |
---|
Retrieves a byte[] value from the preferences.
key | Name of the preference. |
---|---|
defaultValue | the default value if the preference is not found |
Returns the preference values if it exist, or the default value.
The contains(String)
method could check if the preference exists.
key | Name of the preference. |
---|---|
defaultValue | Value to return if this preference does not exist. |
defaultValue
Retrieves an integer value from the preferences.
Returns the preference values if it exist, or the default value. The
contains(String)
method could check if the preference exists.
The call preferences.getInt("key")
is equivalent to :
preferences.getInt("key", 0)
key | Name of the preference. |
---|
Retrieves an long value from the preferences.
Returns the preference values if it exist, or the default value. The
contains(String)
method could check if the preference exists.
The call preferences.getLong("key")
is equivalent to :
preferences.getLong("key", 0)
key | Name of the preference. |
---|
Returns the preference values if it exist, or the default value.
The contains(String)
method could check if the preference exists.
key | Name of the preference. |
---|---|
defaultValue | Value to return if this preference does not exist. |
defaultValue
Retrieve and holds the content of the preferences with the given
name
.
Only one instance of the Preferences is returned for the same name.
If the name does not exist into the persistent storage, then an inexistent and empty preferences object will be returned.
If the preferences object does not exist into the persistent storage, then
calling the exists()
method will return false.
Preferences preferences = Preferences.getPreferences("pref.name"); boolean exist = preferences.exists(); // exist has false value here, // because pref.name has never been saved into application storage before.
name | name of the Preferences object. If the storage does not exist in
the device, the storage data will be created after calling the
save() method. |
---|
Retrieves an String
value from the preferences.
Returns the preference values if it exist, or the default value. The
contains(String)
method could check if the preference exists.
The call preferences.getString("key")
is equivalent to :
preferences.getString("key", null)
key | Name of the preference. |
---|
String
value (null if it does not exist)Returns the preference values if it exist, or the default value.
The contains(String)
method could check if the preference exists.
key | Name of the preference. |
---|---|
defaultValue | Value to return if this preference does not exist. |
defaultValue
Returns if this Preferences object contains changes from the persistent store.
PutXXXX
methods will change this object, this method will return
true if save()
method has not been called to save any changes.
Preferences
are saved
into persistent storage.Set a String value in the application preferences, to be written back once
save()
or cancel()
are called. If the key already exists,
its current value is replaced with the given value
After calling this method, hasChanges()
will return true and the new
inserted data is not really saved into application storage. Any change could
be saved with the save()
method.
key | The name of the preference to modify. |
---|---|
value | The new String value for the preference. |
Set a boolean value in the application preferences, to be written back once
save()
or cancel()
are called. If the key already exists,
its current value is replaced with the given value
After calling this method, hasChanges()
will return true and the new
inserted data is not really saved into application storage. Any change could
be saved with the save()
method.
key | The name of the preference to modify. |
---|---|
value | The new boolean value for the preference. |
Set a byte array in the application preferences, to be written back once
save()
or cancel()
are called. If the key already exists,
its current value is replaced with the given value
After calling this method, hasChanges()
will return true and the new
inserted data is not really saved into application storage. Any change could
be saved with the save()
method.
key | The name of the preference to modify. |
---|---|
value | The new byte array for the preference. |
Set a int value in the application preferences, to be written back once
save()
or cancel()
are called. If the key already exists,
its current value is replaced with the given value
After calling this method, hasChanges()
will return true and the new
inserted data is not really saved into application storage. Any change could
be saved with the save()
method.
key | The name of the preference to modify. |
---|---|
value | The new int value for the preference. |
Set a long value in the application preferences, to be written back once
save()
or cancel()
are called. If the key already exists,
its current value is replaced with the given value
After calling this method, hasChanges()
will return true and the new
inserted data is not really saved into application storage. Any change could
be saved with the save()
method.
key | The name of the preference to modify. |
---|---|
value | The new long value for the preference. |
Deletes from preferences the key entry and its value.
key | key entry to delete |
---|
Removes a preference from the application preferences list.
If a preference with this name does not exist, then nothing is done.
If the preference exists in the persistent storage, it is removed from it permanently.
If the preference object does not exist into the persistent storage (not saved yet), data is also removed.
name | the name of the preference to remove |
---|
Saves (Commit) Preferences
changes.
All modified data (set with put
methods) will be saved. After
calling this method, hasChanges()
will return false.