2017-10-04 207 views
0

我使用下面的代碼獲取姓名&生日。但我還需要電話號碼&電子郵件。如果有人能幫助我,這將是非常棒的。謝謝。Android:從聯繫人中檢索姓名,電話號碼,電子郵件,生日

private void getContacts() { 
    Uri uri = ContactsContract.Data.CONTENT_URI; 

    String[] projection = new String[]{ 
      ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.CommonDataKinds.Event.CONTACT_ID, 
      ContactsContract.CommonDataKinds.Phone.NUMBER, 
      ContactsContract.CommonDataKinds.Email.DATA, 
      ContactsContract.CommonDataKinds.Event.START_DATE 
    }; 

    String where = ContactsContract.Data.MIMETYPE + "= ? AND " + 
      ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
      ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY; 
    String[] selectionArgs = new String[]{ 
      ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE 
    }; 
    String sortOrder = null; 
    ContentResolver contentResolver = this.getActivity().getContentResolver(); 
    Cursor cursor = contentResolver.query(uri, projection, where, selectionArgs, sortOrder); 

    int nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
    int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
    int emailColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); 
    int bithDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE); 
    while (cursor.moveToNext()) { 
     String name = cursor.getString(nameColumn); 
     String number = cursor.getString(numberColumn); 
     String email = cursor.getString(emailColumn); 
     String birthDay = cursor.getString(bithDayColumn); 
     Log.d(TAG, "Birthday: " + birthDay); 
    } 
} 

回答

1

在您的投影你限制你的查詢MIMETYPE CommonDataKinds.Event.CONTENT_ITEM_TYPE唯一的行,所以你只能得到生日。

您需要索取電子郵件和電話mimetypes,但請注意,這些附加信息將在單獨的行中爲同一聯繫人提供。 例如,對於有2部電話,3封電子郵件和生日的聯繫人A,您會在光標中獲得6個結果。因此,您需要使用CONTACT_ID字段將它們全部組合在一起。

下面是簡單的代碼,讓你開始,打印結果的HashMap,你會得到每個聯繫人的所有他/她的名字,電子郵件,電話和生日:

Map<Long, List<String>> contacts = new HashMap<Long, List<String>>(); 

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3}; 

// query only emails/phones/events 
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Event.CONTENT_ITEM_TYPE"', '" + Email.CONTENT_ITEM_TYPE + "')"; 
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null); 

while (cur != null && cur.moveToNext()) { 
    long id = cur.getLong(0); 
    String name = cur.getString(1); // full name 
    String mime = cur.getString(2); // type of data (phone/birthday/email) 
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234 

    String kind = "unknown"; 

    switch (mime) { 
     case Phone.CONTENT_ITEM_TYPE: 
      kind = "phone"; 
      break; 
     case Event.CONTENT_ITEM_TYPE: 
      Event = "birthday"; 
      break; 
     case Email.CONTENT_ITEM_TYPE: 
      Event = "email"; 
      break; 
    } 
    Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data); 

    // add info to existing list if this contact-id was already found, or create a new list in case it's new 
    List<String> infos; 
    if (contacts.containsKey(id)) { 
     infos = contacts.get(id); 
    } else { 
     infos = new ArrayList<String>(); 
     infos.add("name = " + name); 
     contacts.put(id, infos); 
    } 
    infos.add(kind + " = " + data); 
} 
相關問題