2015-11-16 171 views
1

如何獲取在Android中具有電話號碼或電子郵件的聯繫人列表。如何通過電子郵件或電話號碼獲取聯繫人

我對合同有點困惑,我該如何加入信息。

謝謝

+2

查看[this](http://stackoverflow.com/a/26820544/2252830) – pskink

+1

+1對@pskink指出的答案:當我寫這篇評論時,本頁面的解決方案是完全沒有效率,因爲它們使用嵌套查詢。 –

回答

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(); 
      } 
     } 
    } 
+0

僅返回電話號碼的聯繫人。不是電子郵件 –

+0

@JohnSmith我的例子有電話號碼和電子郵件地址。 –

3

這是我如何獲取與電子郵件電話號碼接觸。 聯繫人對象只是我創建的一個簡單的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,我這個使用鏈接。

相關問題