public interface Preferences
The preference data is stored in a file, which matches only one Preferences instance in the memory.
You can use DatabaseHelper.getPreferences(java.lang.String) to obtain the Preferences instance matching
the file that stores preference data, and use DatabaseHelper.removePreferencesFromCache(java.lang.String)
to remove the Preferences instance from the memory.
SystemCapability.Data#DATA_APPDATAMGR| 限定符和类型 | 接口 | 说明 |
|---|---|---|
static interface |
Preferences.PreferencesObserver |
Provides an interface to listen for preference data changes.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
MAX_KEY_LENGTH |
Indicates the maximum length of a key (80 characters).
|
static int |
MAX_VALUE_LENGTH |
Indicates the maximum length of a string (8192 characters).
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
Preferences |
clear() |
Clears all preferences from the
Preferences object. |
Preferences |
delete(String key) |
Deletes the preference with a specified key from the
Preferences object. |
void |
flush() |
Asynchronously saves the
Preferences object to the file. |
boolean |
flushSync() |
Synchronously saves the
Preferences object to the file. |
Map<String,?> |
getAll() |
Obtains all preferences from this
Preferences object. |
boolean |
getBoolean(String key,
boolean defValue) |
Obtains the value of a preference in the boolean format.
|
float |
getFloat(String key,
float defValue) |
Obtains the value of a preference in the float format.
|
int |
getInt(String key,
int defValue) |
Obtains the value of a preference in the int format.
|
long |
getLong(String key,
long defValue) |
Obtains the value of a preference in the long format.
|
String |
getString(String key,
String defValue) |
Obtains the value of a preference in the string format.
|
Set<String> |
getStringSet(String key,
Set<String> defValue) |
Obtains the value of a preference in the string set format.
|
boolean |
hasKey(String key) |
Checks whether the
Preferences object contains a preference matching a specified key. |
Preferences |
putBoolean(String key,
boolean value) |
Sets a boolean value for the key in the
Preferences object. |
Preferences |
putFloat(String key,
float value) |
Sets a float value for the key in the
Preferences object. |
Preferences |
putInt(String key,
int value) |
Sets an int value for the key in the
Preferences object. |
Preferences |
putLong(String key,
long value) |
Sets a long value for the key in the
Preferences object. |
Preferences |
putString(String key,
String value) |
Sets a string value for the key in the
Preferences object. |
Preferences |
putStringSet(String key,
Set<String> value) |
Sets a string set value for the key in the
Preferences object. |
void |
registerObserver(Preferences.PreferencesObserver preferencesObserver) |
Registers an observer to listen for the change of a
Preferences object. |
void |
unregisterObserver(Preferences.PreferencesObserver preferencesObserver) |
Unregisters an existing observer.
|
static final int MAX_KEY_LENGTH
static final int MAX_VALUE_LENGTH
int getInt(String key, int defValue)
If the value is null or not in the int format, the default value is returned.
key - Indicates the key of the preference. It cannot be null or empty.defValue - Indicates the default value to return.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.String getString(String key, String defValue)
If the value is null or not in the string format, the default value is returned.
key - Indicates the key of the preference. It cannot be null or empty.defValue - Indicates the default value to return.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.boolean getBoolean(String key, boolean defValue)
If the value is null or not in the boolean format, the default value is returned.
key - Indicates the key of the preference. It cannot be null or empty.defValue - Indicates the default value to return.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.float getFloat(String key, float defValue)
If the value is null or not in the float format, the default value is returned.
key - Indicates the key of the preference. It cannot be null or empty.defValue - Indicates the default value to return.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.long getLong(String key, long defValue)
If the value is null or not in the long format, the default value is returned.
key - Indicates the key of the preference. It cannot be null or empty.defValue - Indicates the default value to return.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.Set<String> getStringSet(String key, Set<String> defValue)
If the value is null or not in the string set format, the default value is returned.
key - Indicates the key of the preference. It cannot be null or empty.defValue - Indicates the default value to return.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.Map<String,?> getAll()
Preferences object.
Do not modify any of the returned preferences; otherwise, data inconsistency will occur.
Preferences object.Preferences putInt(String key, int value)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to modify. It cannot be null or empty.value - Indicates the value of the preference.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences putString(String key, String value)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to modify. It cannot be null or empty.value - Indicates the value of the preference.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences putBoolean(String key, boolean value)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to modify. It cannot be null or empty.value - Indicates the value of the preference.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences putLong(String key, long value)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to modify. It cannot be null or empty.value - Indicates the value of the preference.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences putFloat(String key, float value)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to modify. It cannot be null or empty.value - Indicates the value of the preference.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences putStringSet(String key, Set<String> value)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to modify. It cannot be null or empty.value - Indicates the value of the preference.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences delete(String key)
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
key - Indicates the key of the preference to delete. It cannot be null or empty.Preferences object.IllegalArgumentException - Throws this exception if key.length() exceeds
MAX_KEY_LENGTH.Preferences clear()
Preferences object.
You can call the flush() or flushSync() method to save the Preferences object to the
file.
Preferences object.void flush()
Preferences object to the file.boolean flushSync()
Preferences object to the file.true if the Preferences instance is saved successfully;
returns false otherwise.boolean hasKey(String key)
Preferences object contains a preference matching a specified key.key - Indicates the key of the preference to check for.true if the Preferences object contains a preference with the specified key;
returns false otherwise.IllegalArgumentException - Throws this exception if key.length() exceeds MAX_KEY_LENGTH.void registerObserver(Preferences.PreferencesObserver preferencesObserver)
Preferences object.
The observer of Preferences must implement the PreferencesObserver interface.
When the preference data changes, PreferencesObserver#onChange will be called.
preferencesObserver - Indicates the observer to be registered.void unregisterObserver(Preferences.PreferencesObserver preferencesObserver)
The observer of Preferences must implement the PreferencesObserver interface.
preferencesObserver - Indicates the observer to be unregistered.