openharmony 鸿蒙 capi-oh-preferences-h

2026-08-25 浏览 (1)

oh_preferences.h

Overview

Provides APIs and structs for accessing the Preferences object.

File to include: <database/preferences/oh_preferences.h>

Library: libohpreferences.so

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Since: 13

Related module: Preferences

Summary

Structs

Nametypedef KeywordDescription
OH_PreferencesOH_PreferencesDefines a struct for a Preferences object.

Functions

Nametypedef KeywordDescription
typedef void (*OH_PreferencesDataObserver)(void *context, const OH_PreferencesPair *pairs, uint32_t count)OH_PreferencesDataObserverDefines a struct for the callback for data changes.
OH_Preferences *OH_Preferences_Open(OH_PreferencesOption *option, int *errCode)-Opens a Preferences instance and creates a pointer to it.
If this pointer is no longer required, use OH_Preferences_Close to close the instance.
int OH_Preferences_Close(OH_Preferences *preference)-Closes a Preferences instance.
int OH_Preferences_GetInt(OH_Preferences *preference, const char *key, int *value)-Obtains an integer corresponding to the specified key in a Preferences instance.
int OH_Preferences_GetBool(OH_Preferences *preference, const char *key, bool *value)-Obtains a Boolean value corresponding to the specified key in a Preferences instance.
int OH_Preferences_GetString(OH_Preferences *preference, const char *key, char **value, uint32_t *valueLen)-Obtains a string corresponding to the specified key in a Preferences instance.
void OH_Preferences_FreeString(char *string)-Releases a string obtained from a Preferences instance.
int OH_Preferences_SetInt(OH_Preferences *preference, const char *key, int value)-Sets an integer based on the specified key in a Preferences instance.
int OH_Preferences_SetBool(OH_Preferences *preference, const char *key, bool value)-Sets a Boolean value based on the specified key in a Preferences instance.
int OH_Preferences_SetString(OH_Preferences *preference, const char *key, const char *value)-Sets a string based on the specified key in a Preferences instance.
int OH_Preferences_Delete(OH_Preferences *preference, const char *key)-Deletes the KV data corresponding to the specified key from a Preferences instance.
int OH_Preferences_RegisterDataObserver(OH_Preferences *preference, void *context,OH_PreferencesDataObserver observer, const char *keys[], uint32_t keyCount)-Subscribes to data changes of the specified keys. If the value of the specified key changes, a callback will be invoked after OH_Preferences_Close() is called.
int OH_Preferences_UnregisterDataObserver(OH_Preferences *preference, void *context,OH_PreferencesDataObserver observer, const char *keys[], uint32_t keyCount)-Unsubscribes from data changes of the specified keys.
int OH_Preferences_IsStorageTypeSupported(Preferences_StorageType type, bool *isSupported)-Checks whether the specified storage type is supported.
int OH_Preferences_DeletePreferences(OH_PreferencesOption *option)-Deletes the specified Preferences object.
int OH_Preferences_SetValue(OH_Preferences *preference, const char *key, OH_PreferencesValue *value)-Sets OH_PreferencesValue in the Preferences object.
int OH_Preferences_GetValue(OH_Preferences *preference, const char *key, OH_PreferencesValue **value)-Obtains the value from the Preferences object based on the given key.
int OH_Preferences_GetAll(OH_Preferences *preference, OH_PreferencesPair **pairs, uint32_t *count)-Obtains all the values from the Preferences object.
bool OH_Preferences_HasKey(OH_Preferences *preference, const char *key)-Checks whether the Preferences object contains KV data matching the specified key. Returns true if present, and false otherwise.
int OH_Preferences_Flush(OH_Preferences *preference)-Saves the cache of the OH_Preferences object to an XML file.
int OH_Preferences_ClearCache(OH_Preferences *preference)-Clears all values in the cache of the OH_Preferences object.
int OH_Preferences_RegisterMultiProcessDataObserver(OH_Preferences *preference, void *context, OH_PreferencesDataObserver observer)-Registers a multi-process data observer for the Preferences object.
int OH_Preferences_UnregisterMultiProcessDataObserver(OH_Preferences *preference, void *context, OH_PreferencesDataObserver observer)-Unregisters the multi-process data observer of the Preferences object.

Function Description

OH_PreferencesDataObserver()

typedef void (*OH_PreferencesDataObserver)(void *context, const OH_PreferencesPair *pairs, uint32_t count)

Description

Defines a struct for the callback for data changes.

Since: 13

Parameters

NameDescription
void *contextPointer to the application context.
const OH_PreferencesPair *pairsPointer to the changed KV data.
uint32_t countNumber of KV pairs changed.

OH_Preferences_Open()

OH_Preferences *OH_Preferences_Open(OH_PreferencesOption *option, int *errCode)

Description

Opens a Preferences instance and creates a pointer to it.
If this pointer is no longer required, use OH_Preferences_Close to close the instance.

Since: 13

Parameters

NameDescription
OH_PreferencesOption *optionPointer to the OH_PreferencesOption instance.
int *errCodePointer to the error code returned. For details, see OH_Preferences_ErrCode.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_NOT_SUPPORTED indicates the system capability is not supported.
PREFERENCES_ERROR_DELETE_FILE indicates the file fails to be deleted.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

Returns

TypeDescription
OH_PreferencesReturns a pointer to the OH_Preferences instance opened if the operation is successful; returns a null pointer otherwise.

OH_Preferences_Close()

int OH_Preferences_Close(OH_Preferences *preference)

Description

Closes a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance to close.

Returns

TypeDescription
intReturns an error code. For details, see OH_Preferences_ErrCode.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_GetInt()

int OH_Preferences_GetInt(OH_Preferences *preference, const char *key, int *value)

Description

Obtains an integer corresponding to the specified key in a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the value to obtain.
int *valuePointer to the integer value obtained.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_KEY_NOT_FOUND indicates the specified key does not exist.

OH_Preferences_GetBool()

int OH_Preferences_GetBool(OH_Preferences *preference, const char *key, bool *value)

Description

Obtains a Boolean value corresponding to the specified key in a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the value to obtain.
bool *valuePointer to the Boolean value obtained.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_KEY_NOT_FOUND indicates the specified key does not exist.

OH_Preferences_GetString()

int OH_Preferences_GetString(OH_Preferences *preference, const char *key, char **value, uint32_t *valueLen)

Description

Obtains a string corresponding to the specified key in a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the value to obtain.
char **valueDouble pointer to the string obtained. If the string is not required, you can use OH_Preferences_FreeString to free the string (release the memory occupied by the string).
uint32_t *valueLenPointer to the length of the string obtained.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_KEY_NOT_FOUND indicates the specified key does not exist.

OH_Preferences_FreeString()

void OH_Preferences_FreeString(char *string)

Description

Releases a string obtained from a Preferences instance.

Since: 13

Parameters

NameDescription
char *stringPointer to the string to release.

OH_Preferences_SetInt()

int OH_Preferences_SetInt(OH_Preferences *preference, const char *key, int value)

Description

Sets an integer based on the specified key in a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the value to set.
int valueInteger value to be set.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_SetBool()

int OH_Preferences_SetBool(OH_Preferences *preference, const char *key, bool value)

Description

Sets a Boolean value based on the specified key in a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the value to set.
bool valueBoolean value to be set.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_SetString()

int OH_Preferences_SetString(OH_Preferences *preference, const char *key, const char *value)

Description

Sets a string based on the specified key in a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the value to set.
const char *valuePointer to the string to set.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_Delete()

int OH_Preferences_Delete(OH_Preferences *preference, const char *key)

Description

Deletes the KV data corresponding to the specified key from a Preferences instance.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
const char *keyPointer to the key of the KV pair to delete.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_RegisterDataObserver()

int OH_Preferences_RegisterDataObserver(OH_Preferences *preference, void *context,OH_PreferencesDataObserver observer, const char *keys[], uint32_t keyCount)

Description

Subscribes to data changes of the specified keys. If the value of the specified key changes, a callback will be invoked after OH_Preferences_Close() is called.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
void *contextPointer to the application context.
OH_PreferencesDataObserver observerOH_PreferencesDataObserver callback to be invoked when data changes.
const char *keys[]Array of keys.
uint32_t keyCountNumber of keys.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_GET_DATAOBSMGRCLIENT indicates a failure in obtaining the data change subscription service.

OH_Preferences_UnregisterDataObserver()

int OH_Preferences_UnregisterDataObserver(OH_Preferences *preference, void *context,OH_PreferencesDataObserver observer, const char *keys[], uint32_t keyCount)

Description

Unsubscribes from data changes of the specified keys.

Since: 13

Parameters

NameDescription
OH_Preferences *preferencePointer to the OH_Preferences instance.
void *contextPointer to the application context.
OH_PreferencesDataObserver observerOH_PreferencesDataObserver callback to be invoked when data changes.
const char *keys[]Pointer to the keys whose changes are not observed.
uint32_t keyCountNumber of keys.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates the storage is abnormal.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_IsStorageTypeSupported()

int OH_Preferences_IsStorageTypeSupported(Preferences_StorageType type, bool *isSupported)

Since: 18

Parameters

NameDescription
Preferences_StorageType typeStorage type to check.
bool *isSupportedPointer to the check result. The value true means the storage type is supported; the value false means the opposite.

Returns

TypeDescription
intReturn an operation status code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.

OH_Preferences_DeletePreferences()

int OH_Preferences_DeletePreferences(OH_PreferencesOption *option)

Description

Deletes the specified Preferences object.

Since: 23

Parameters

NameDescription
OH_PreferencesOption *optionPointer to the OH_PreferencesOption instance.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_NOT_SUPPORTED indicates the system capability is not supported.
PREFERENCES_ERROR_DELETE_FILE indicates the file fails to be deleted.

OH_Preferences_SetValue()

int OH_Preferences_SetValue(OH_Preferences *preference, const char *key, OH_PreferencesValue *value)

Description

Sets OH_PreferencesValue in the Preferences object.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.
const char *keyPointer to the key of the value to set.
OH_PreferencesValue *valuePointer to the OH_PreferencesValue value to set.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates a storage exception.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

OH_Preferences_GetValue()

int OH_Preferences_GetValue(OH_Preferences *preference, const char *key, OH_PreferencesValue **value)

Description

Obtains the value from the Preferences object based on the given key.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.
const char *keyPointer to the key of the value to obtain.
OH_PreferencesValue **valueDouble pointer to OH_PreferencesValue.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates a storage exception.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_KEY_NOT_FOUND indicates the specified key does not exist.

OH_Preferences_GetAll()

int OH_Preferences_GetAll(OH_Preferences *preference, OH_PreferencesPair **pairs, uint32_t *count)

Description

Obtains all the values from the Preferences object.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.
OH_PreferencesPair **pairsPointer to the KV data to obtain. When the KV data is no longer needed, call OH_Preferences_FreeString to free the memory.
uint32_t *countPointer to the count of all obtained values.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates a storage exception.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_KEY_NOT_FOUND indicates the specified key does not exist.

OH_Preferences_HasKey()

bool OH_Preferences_HasKey(OH_Preferences *preference, const char *key)

Description

Checks whether the Preferences object contains KV data matching the specified key. Returns true if present, and false otherwise.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.
const char *keyPointer to the key to check.

Returns

TypeDescription
boolReturns true if the Preferences object contains KV data that matches the specified key; returns false otherwise.

OH_Preferences_Flush()

int OH_Preferences_Flush(OH_Preferences *preference)

Description

Saves the cache of the OH_Preferences object to an XML file.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_NOT_SUPPORTED indicates the system capability is not supported.

OH_Preferences_ClearCache()

int OH_Preferences_ClearCache(OH_Preferences *preference)

Description

Clears all values in the cache of the OH_Preferences object.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_NOT_SUPPORTED indicates the system capability is not supported.

OH_Preferences_RegisterMultiProcessDataObserver()

int OH_Preferences_RegisterMultiProcessDataObserver(OH_Preferences *preference, void *context, OH_PreferencesDataObserver observer)

Description

Registers a multi-process data observer for the Preferences object.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.
void *contextPointer to the data observer context.
OH_PreferencesDataObserver observerThe OH_PreferencesDataObserver callback function to register.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates a storage exception.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.
PREFERENCES_ERROR_GET_DATAOBSMGRCLIENT indicates a failure in obtaining the data change subscription service.

OH_Preferences_UnregisterMultiProcessDataObserver()

int OH_Preferences_UnregisterMultiProcessDataObserver(OH_Preferences *preference, void *context, OH_PreferencesDataObserver observer)

Description

Unregisters the multi-process data observer of the Preferences object.

Since: 23

Parameters

NameDescription
OH_Preferences *preferencePointer to the target OH_Preferences instance.
void *contextPointer to the data observer context.
OH_PreferencesDataObserver observerThe OH_PreferencesDataObserver callback function to unregister.

Returns

TypeDescription
intReturns an error code.
PREFERENCES_OK indicates the operation is successful.
PREFERENCES_ERROR_INVALID_PARAM indicates invalid parameters are specified.
PREFERENCES_ERROR_STORAGE indicates a storage exception.
PREFERENCES_ERROR_MALLOC indicates a failure in memory allocation.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-distributedKVStore-sys

openharmony 鸿蒙 capi-udmf-oh-udshyperlink

openharmony 鸿蒙 js-apis-data-dataSharePredicates

openharmony 鸿蒙 capi-udmf-oh-udsappitem

openharmony 鸿蒙 capi-rdb-oh-data-vbuckets

openharmony 鸿蒙 capi-rdb-oh-predicates

openharmony 鸿蒙 capi-rdb-oh-data-value

openharmony 鸿蒙 capi-preferences-oh-preferencespair

openharmony 鸿蒙 errorcode-datashare

openharmony 鸿蒙 arkts-apis-data-relationalStore-Transaction

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/oTInE9UJ