如何通过电子邮件或电话号码获取联系人

时间:2015-11-16 17:15:55

标签: android

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

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

谢谢

2 个答案:

答案 0 :(得分:8)

以下是我使用电子邮件电话号码获取联系人的方式。 联系对象只是我创建的一个简单的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的链接。

答案 1 :(得分:1)

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();
            }
        }
    }
相关问题