【问题标题】:How to get contacts with email or phone number如何通过电子邮件或电话号码获取联系人
【发布时间】:2016-11-11 11:30:06
【问题描述】:

如何在 Android 中获取具有电话号码或电子邮件的联系人列表。

我对合同以及如何加入信息有点困惑。

谢谢

【问题讨论】:

  • this
  • +1 对@pskink 指出的答案:在我写这篇评论的时候,这个页面中的解决方案是完全低效的,因为它们使用嵌套查询。

标签: android


【解决方案1】:

以下是我使用电子邮件电话号码获取联系人的方法。 Contact 对象只是我创建的一个简单的 pojo。此代码位于我在用户提供访问联系人的权限后运行的 AsyncTask 中。

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {

  do {
      // get the contact's information
      String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
      String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

      // get the user's email address
      String email = null;
      Cursor ce = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
              ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
      if (ce != null && ce.moveToFirst()) {
          email = ce.getString(ce.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
          ce.close();
      }

      // get the user's phone number
      String phone = null;
      if (hasPhone > 0) {
          Cursor cp = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
          if (cp != null && cp.moveToFirst()) {
              phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              cp.close();
          }
      }

      // if the user user has an email or phone then add it to contacts
      if ((!TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
              && !email.equalsIgnoreCase(name)) || (!TextUtils.isEmpty(phone))) {
          Contact contact = new Contact();
          contact.name = name;
          contact.email = email;
          contact.phoneNumber = phone;
          contacts.add(contact);
      }

  } while (cursor.moveToNext());

  // clean up cursor
  cursor.close();
}    

对于 DISPLAY_NAME,您可以使用以下内容:

private final String DISPLAY_NAME = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;

这是我使用的AsyncTask Example 的链接。

【讨论】:

  • 答案很好,现在你有 10k 的声誉 :) 谢谢
【解决方案2】:
ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
              String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
              String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
              if (Integer.parseInt(cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                 Cursor pCur = cr.query(
                           ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                           null,
                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                           new String[]{id}, null);
                 while (pCur.moveToNext()) {
                     String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                 }
                pCur.close();
            }
        }
    }

【讨论】:

  • 只返回电话号码的联系人。不是电子邮件
  • @JohnSmith 我的示例有电话号码和电子邮件地址。
猜你喜欢
  • 1970-01-01
  • 2016-12-26
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2011-07-30
  • 1970-01-01
  • 2015-06-19
相关资源
最近更新 更多