How to read/write simple key value pair data via preference API

This sample code describes how to read/write data on device persistently via the preference API.
const char *string_key = "string_key";
const char *string_value = "Sample content";
char *string_output;
const char *integer_key = "integer_key";
int integer_value = 1;
int integer_output;
bool existing;

existing = false;
if(preference_is_existing(string_key, &existing) == 0 && existing){
preference_get_string(string_key, &string_output);
        dlog_print(DLOG_INFO, LOG_TAG, "[test_preference]string: %s.\n", string_output);
        free(string_output);
} else {
        preference_set_string(string_key, string_value);
}

existing = false;
if(preference_is_existing(integer_key, &existing) == 0 && existing){
        preference_get_int(integer_key, &integer_output);
        dlog_print(DLOG_INFO, LOG_TAG, "[test_preference]integer: %d.\n", integer_output);
} else {
        preference_set_int(integer_key, integer_value);
}

Responses

0 Replies