Android: Retrieve Name, Phone Number, Email, Birthday from Contact

天涯浪子 提交于 2019-11-27 08:44:29

问题


I am getting only name & birthday using the code below. But I need phone number & email also. It would be great if anyone can help me out. Thanks.

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:


In your projection you're limiting your query to rows of MIMETYPE CommonDataKinds.Event.CONTENT_ITEM_TYPE only, so you'll only get birthdays.

You need to ask for emails and phones mimetypes, but note that these additional information will come in separate rows for the same contact. For example, for contact A that has 2 phones, 3 emails and a birthday, you'll get 6 results in your cursor. So you need to group them all together using the CONTACT_ID field.

Here's simple code to get you started, print the resulting HashMap and you'll get for each contact all his/hers name, emails, phones and birthday:

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 + "')";
ContentResolver cr = getContentResolver();
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: 
            kind = "birthday";
            break;
        case Email.CONTENT_ITEM_TYPE: 
            kind = "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);
}


来源:https://stackoverflow.com/questions/46572169/android-retrieve-name-phone-number-email-birthday-from-contact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!