3

我需要從設備讀取聯繫人。我需要的字段是ID,Display Name,Phone Number(所有類型)Email id(所有類型)和'照片'。現在我正在這樣做。如何使用單個查詢從內容提供者檢索所有聯繫人詳細信息?

1:首先我讀所有從ContactsContract.Contacts.CONTENT_URI;id S作爲以下所示

 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:然後通過這個光標迭代讀取所有聯繫人的requred字段。

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

'ContentProvider'和'ContentResolver'不支持'JOIN',部分原因是不要求供應商是由SQL數據庫支持或其他能夠理解「連接」的東西。 – CommonsWare

回答

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 + ")"); 
} 
相關問題