找回密码
 立即注册
查看: 2290|回复: 0

检索所有联系人的名字和他们的默认的电话号码的代码

[复制链接]
发表于 2015-8-16 14:40:10 | 显示全部楼层 |阅读模式
这个代码片段检索所有联系人显示每个联系人的姓名和电话号码,如果它被定义的。 如果定义了多个电话号码联系,只有默认了。 需要设置在tizen-manifest设置。 xml文件:http://tizen.org/privilege/contact.read
  1. //    PRIVILEGE NEEDED to be set in tizen-manifest.xml file:
  2. //    http://tizen.org/privilege/contact.read

  3. #include <contacts.h>
  4. #include <stdlib.h>
  5. #include <dlog.h> // for logging purposes

  6. contacts_list_h contacts_list;
  7. int err = CONTACTS_ERROR_NONE;

  8. if (contacts_connect() == CONTACTS_ERROR_NONE)
  9. {
  10.     if (contacts_db_get_all_records(_contacts_contact._uri, 0, 0, &contacts_list) == CONTACTS_ERROR_NONE)
  11.     {
  12.         LOGI ("Listing contacts...");
  13.         contacts_record_h contact;
  14.         while (contacts_list_get_current_record_p(contacts_list, &contact) == CONTACTS_ERROR_NONE)
  15.         {
  16.             // Contact found.
  17.             bool is_phone_number = false;
  18.             char *contact_name = NULL, *contact_number = NULL;
  19.             err = contacts_record_get_str(contact, _contacts_contact.display_name, &contact_name);
  20.             if (err != CONTACTS_ERROR_NONE)
  21.             {
  22.                 LOGE ("Error occurred when trying to get contact's display name! Error code: %d", err);
  23.                 contact_name = strdup("Not defined");
  24.             }
  25.             // Notice that if you use the default Contacts app
  26.             // to add a contact without a name and only with phone number,
  27.             // the display name retrieved will be the phone number itself.

  28.             contacts_record_h child_record_phone;
  29.             int child_records_count = 0;

  30.             err = contacts_record_get_child_record_count(contact, _contacts_contact.number, &child_records_count);
  31.             if (err == CONTACTS_ERROR_NONE)
  32.             {
  33.                 if (child_records_count == 1) // Only one number found
  34.                 {
  35.                     err = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, 0, &child_record_phone);
  36.                     if (err == CONTACTS_ERROR_NONE)
  37.                     {
  38.                         err = contacts_record_get_str(child_record_phone, _contacts_number.number, &contact_number);
  39.                         if (err == CONTACTS_ERROR_NONE)
  40.                         {
  41.                             // Successfully retrieved the phone number
  42.                             // so we set the bool to know that we don't need a placeholder.
  43.                             is_phone_number = true;
  44.                         }
  45.                         else
  46.                         {
  47.                             LOGE ("Error occurred when getting the phone number! Error code: %d", err);
  48.                         }
  49.                     }
  50.                     else
  51.                     {
  52.                         LOGE ("Error occurred when getting the phone number record! Error code: %d", err);
  53.                     }
  54.                 }
  55.                 else if (child_records_count > 1) // If there is more than one phone number we will display only the default one.
  56.                 {
  57.                     int i;
  58.                     for (i = 0; i < child_records_count; i++)
  59.                     {
  60.                         err = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &child_record_phone);
  61.                         if (err == CONTACTS_ERROR_NONE)
  62.                         {
  63.                             bool is_default = false;
  64.                             err = contacts_record_get_bool(child_record_phone, _contacts_number.is_default, &is_default);
  65.                             if (err == CONTACTS_ERROR_NONE)
  66.                             {
  67.                                 if (is_default == false)
  68.                                 {
  69.                                     LOGI ("Many numbers were defined for this contact and this is not a default one. We ignore this number.");
  70.                                 }
  71.                                 else
  72.                                 {
  73.                                     //Default phone number found
  74.                                     err = contacts_record_get_str(child_record_phone, _contacts_number.number, &contact_number);
  75.                                     if (err == CONTACTS_ERROR_NONE)
  76.                                     {
  77.                                         // Successfully retrieved the phone number
  78.                                         // so we set the bool to know that we don't need a placeholder.
  79.                                         is_phone_number = true;
  80.                                         break; //We have found a default number, we can finish browsing.
  81.                                     }
  82.                                     else
  83.                                     {
  84.                                         LOGE ("Error occurred when getting the phone number! Error code: %d", err);
  85.                                     }
  86.                                 }
  87.                             }
  88.                             else
  89.                             {
  90.                                 LOGE ("Error occurred when getting the 'is default' attribute! Error code: %d", err);
  91.                             }
  92.                         }
  93.                         else
  94.                         {
  95.                             LOGE ("Error occurred when getting the phone number record! Error code: %d", err);
  96.                         }
  97.                     }
  98.                 }
  99.                 else
  100.                 {
  101.                     // No phone numbers were found for the specified contact.
  102.                     // This can happen for all contacts where no phone number was defined.
  103.                     // You can do something specific to the no-phone contacts, for example filter them.
  104.                     // In this snippet we have already set the bool is_phone_number to false by default,
  105.                     // so we do nothing here.
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 LOGE ("Error occurred when getting the count of phone numbers! Error code: %d", err);
  111.             }
  112.             if(is_phone_number == false)
  113.             {
  114.                 // we set the placeholder in place of the phone number.
  115.                 contact_number = strdup("Not defined");
  116.             }

  117.             // Here you can make use of the retrieved contact name and phone number.
  118.             // In this snippet it's only printed as a log:
  119.             LOGI("Contact name: %s,\t Phone number: %s", contact_name, contact_number);

  120.             free(contact_name);
  121.             free(contact_number);

  122.             contacts_list_next(contacts_list); // proceed to next contact on a list
  123.         }
  124.         LOGI ("Listing contacts completed!");

  125.         contacts_list_destroy(contacts_list, true);
  126.     }
  127.     else
  128.     {
  129.         LOGE ("Error occurred when getting the contact list! Error code: %d", err);
  130.     }

  131.     if (contacts_disconnect() != CONTACTS_ERROR_NONE)
  132.     {
  133.         LOGE ("Error occurred when disconnecting from contacts service!");
  134.     }
  135. }
  136. else
  137. {
  138.     LOGE ("Error occurred when connecting to contacts service! Error code: %d", err);
  139. }
复制代码
欢迎来到泰泽网:http://www.tizennet.com/ 泰泽论坛:http://bbs.tizennet.com/ 好没有内涵哦,快到设置中更改这个无聊的签名吧!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|泰泽应用|泰泽论坛|泰泽网|小黑屋|Archiver|手机版|泰泽邮箱|泰泽网 ( 蜀ICP备13024062号-1 )

GMT+8, 2024-4-26 03:07 , Processed in 0.054270 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表