|
这个代码片段检索所有联系人显示每个联系人的姓名和电话号码,如果它被定义的。 如果定义了多个电话号码联系,只有默认了。 需要设置在tizen-manifest设置。 xml文件:http://tizen.org/privilege/contact.read
- // PRIVILEGE NEEDED to be set in tizen-manifest.xml file:
- // http://tizen.org/privilege/contact.read
- #include <contacts.h>
- #include <stdlib.h>
- #include <dlog.h> // for logging purposes
- contacts_list_h contacts_list;
- int err = CONTACTS_ERROR_NONE;
- if (contacts_connect() == CONTACTS_ERROR_NONE)
- {
- if (contacts_db_get_all_records(_contacts_contact._uri, 0, 0, &contacts_list) == CONTACTS_ERROR_NONE)
- {
- LOGI ("Listing contacts...");
- contacts_record_h contact;
- while (contacts_list_get_current_record_p(contacts_list, &contact) == CONTACTS_ERROR_NONE)
- {
- // Contact found.
- bool is_phone_number = false;
- char *contact_name = NULL, *contact_number = NULL;
- err = contacts_record_get_str(contact, _contacts_contact.display_name, &contact_name);
- if (err != CONTACTS_ERROR_NONE)
- {
- LOGE ("Error occurred when trying to get contact's display name! Error code: %d", err);
- contact_name = strdup("Not defined");
- }
- // Notice that if you use the default Contacts app
- // to add a contact without a name and only with phone number,
- // the display name retrieved will be the phone number itself.
- contacts_record_h child_record_phone;
- int child_records_count = 0;
- err = contacts_record_get_child_record_count(contact, _contacts_contact.number, &child_records_count);
- if (err == CONTACTS_ERROR_NONE)
- {
- if (child_records_count == 1) // Only one number found
- {
- err = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, 0, &child_record_phone);
- if (err == CONTACTS_ERROR_NONE)
- {
- err = contacts_record_get_str(child_record_phone, _contacts_number.number, &contact_number);
- if (err == CONTACTS_ERROR_NONE)
- {
- // Successfully retrieved the phone number
- // so we set the bool to know that we don't need a placeholder.
- is_phone_number = true;
- }
- else
- {
- LOGE ("Error occurred when getting the phone number! Error code: %d", err);
- }
- }
- else
- {
- LOGE ("Error occurred when getting the phone number record! Error code: %d", err);
- }
- }
- else if (child_records_count > 1) // If there is more than one phone number we will display only the default one.
- {
- int i;
- for (i = 0; i < child_records_count; i++)
- {
- err = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &child_record_phone);
- if (err == CONTACTS_ERROR_NONE)
- {
- bool is_default = false;
- err = contacts_record_get_bool(child_record_phone, _contacts_number.is_default, &is_default);
- if (err == CONTACTS_ERROR_NONE)
- {
- if (is_default == false)
- {
- LOGI ("Many numbers were defined for this contact and this is not a default one. We ignore this number.");
- }
- else
- {
- //Default phone number found
- err = contacts_record_get_str(child_record_phone, _contacts_number.number, &contact_number);
- if (err == CONTACTS_ERROR_NONE)
- {
- // Successfully retrieved the phone number
- // so we set the bool to know that we don't need a placeholder.
- is_phone_number = true;
- break; //We have found a default number, we can finish browsing.
- }
- else
- {
- LOGE ("Error occurred when getting the phone number! Error code: %d", err);
- }
- }
- }
- else
- {
- LOGE ("Error occurred when getting the 'is default' attribute! Error code: %d", err);
- }
- }
- else
- {
- LOGE ("Error occurred when getting the phone number record! Error code: %d", err);
- }
- }
- }
- else
- {
- // No phone numbers were found for the specified contact.
- // This can happen for all contacts where no phone number was defined.
- // You can do something specific to the no-phone contacts, for example filter them.
- // In this snippet we have already set the bool is_phone_number to false by default,
- // so we do nothing here.
- }
- }
- else
- {
- LOGE ("Error occurred when getting the count of phone numbers! Error code: %d", err);
- }
- if(is_phone_number == false)
- {
- // we set the placeholder in place of the phone number.
- contact_number = strdup("Not defined");
- }
- // Here you can make use of the retrieved contact name and phone number.
- // In this snippet it's only printed as a log:
- LOGI("Contact name: %s,\t Phone number: %s", contact_name, contact_number);
- free(contact_name);
- free(contact_number);
- contacts_list_next(contacts_list); // proceed to next contact on a list
- }
- LOGI ("Listing contacts completed!");
- contacts_list_destroy(contacts_list, true);
- }
- else
- {
- LOGE ("Error occurred when getting the contact list! Error code: %d", err);
- }
- if (contacts_disconnect() != CONTACTS_ERROR_NONE)
- {
- LOGE ("Error occurred when disconnecting from contacts service!");
- }
- }
- else
- {
- LOGE ("Error occurred when connecting to contacts service! Error code: %d", err);
- }
复制代码 |
|