如何通过单个查询从内容提供商检索所有联系人详细信息?

时间:2016-03-12 15:24:18

标签: android android-contacts android-contentresolver contactscontract

我需要从设备上读取联系人。我需要的字段为IDDisplay NamePhone Number(所有类型)Email id(所有类型)和“照片”。为此我现在就是这样做的。

1:首先我正在阅读id中的所有ContactsContract.Contacts.CONTENT_URI;,如下所示

        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
        // Querying the table ContactsContract.Contacts to retrieve all the contacts
        String[] projection = {ID};
        Cursor contactsCursor = mContentResolver.query(contactsUri, projection, null, null,
                "upper(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

2:然后迭代此光标以读取所有联系人的所需字段。

if (contactsCursor.moveToFirst()) {
            do {
                 long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex(ID));
                   Uri dataUri = ContactsContract.Data.CONTENT_URI;
                // Querying the table ContactsContract.Data to retrieve individual items like
                // home phone, mobile phone, work email etc corresponding to each contact
                   String[] columns = {CONTACT_ID, PHOTO_URI, DISPLAY_NAME, MIME_TYPE, DATA_1, DATA_2, DATA_4};
                   String selection = ContactsContract.Data.CONTACT_ID + "=" + contactId;
                   Cursor dataCursor = mContentResolver.query(dataUri, columns,
                        selection,
                        null, null);
                                 if (dataCursor.moveToFirst()) {
                                 // Getting Display Name
                                 displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                                 do {
                                    // Getting Phone numbers
                                    String mimeType = dataCursor.getString(dataCursor.getColumnIndex(MIME_TYPE));
                                    switch (mimeType) {
                                             case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                                             String phoneNumber = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
                                             switch (dataCursor.getInt(dataCursor.getColumnIndex(DATA_2))) {
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                        homePhone = phoneNumber;
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                        mobilePhone = phoneNumber;
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                        workPhone = phoneNumber;
                                        break;
                                    default:
                                        otherPhone = phoneNumber;
                                }
                                break;
                              // Getting emails
                              case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE:
                                switch (dataCursor.getInt(dataCursor.getColumnIndex(DATA_2))) {
                                    case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
                                        homeEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
                                        break;
                                    case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
                                        workEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
                                        break;
                                    default:
                                        otherEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
                                }
                                break;
                               // getting photo Uri 
                               case ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE:
                                if (dataCursor.getString(dataCursor.getColumnIndex(PHOTO_URI)) != null) {
                                    photoUri = Uri.parse(dataCursor.getString(dataCursor.getColumnIndex(PHOTO_URI)));
                                }
                                break;
                        }

                    } while (dataCursor.moveToNext());


               } while (contactsCursor.moveToNext());

查询工作正常,但问题是需要花费太多时间来迭代并获取每个联系人的详细信息。第一个查询正在快速返回,但整个延迟现在位于第二部分,即迭代第一个查询的每一行并查询每个字段。这可以在单个连接查询中完成,以便我可以优化性能吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以在一个查询中执行此操作 - 有关联系人的所有数据实际上都存储在一个表Data中,其中包含您在联系人上需要的所有内容,包括CONTACT_ID和DISPLAY_NAME Contacts表。

因此,您需要在单个查询中基本从数据表中查询所有内容,并使用从CONTACT_ID到某个联系人对象的HashMap将其排序为联系人(或者如果您更喜欢详细信息列表)。

(注意:请确保仅从ContactsContract包导入以下类)

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, Data.PHOTO_ID};
String selection = Data.MIMETYPE + " IN ('" + Phone.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);
    String mime = cur.getString(2); // email / phone / company
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
    int type = cur.getInt(4); // a numeric value representing type: e.g. home / office / personal
    String label = cur.getString(5); // a custom label in case type is "TYPE_CUSTOM"
    long photoId = cur.getLong(6);

    String kind = "unknown";
    String labelStr = "";

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

    // 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);
        infos.add("photo = " + photoId);
        contacts.put(id, infos);
    }
    infos.add(kind + " = " + data + " (" + labelStr + ")");
}