如何在一个 SQLite 查询中获取联系人 ID、电子邮件、电话号码?通讯录 Android 优化

问题描述 投票:0回答:2

我想至少使用一个电话号码获取所有联系人,我还想要每个联系人的所有电话号码和所有电子邮件。

当前代码:

// To get All Contacts having atleast one phone number.

Uri uri = ContactsContract.Contacts.CONTENT_URI;
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " > ?";
String[] selectionArgs = new String[] {"0"};
Cursor cu = applicationContext.getContentResolver().query(uri, 
                null, selection, selectionArgs, null);

// For getting All Phone Numbers and Emails further queries : 
while(cu.moveToNext()){
String id = cu.getString(cu.getColumnIndex(ContactsContract.Contacts._ID));


 // To get Phone Numbers of Contact
    Cursor pCur = context.getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
 new String[]{id}, null);

// To get Email ids of Contact
Cursor emailCur = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null); 

// Iterate through these cursors to get Phone numbers and Emails
}

如果我的设备中有超过 1000 个联系人,则需要花费太多时间。如何在单个查询中获取所有数据,而不是为每个联系人执行两次额外的查询?

或者还有其他优化方法吗?

提前谢谢您。

android sqlite android-contacts contactscontract
2个回答
49
投票

ICS:当您从

Data.CONTENT_URI
查询时,关联的
Contact
中的所有行都已加入 - 即这将起作用:

Java:

    ContentResolver resolver = getContentResolver();
    Cursor c = resolver.query(
            Data.CONTENT_URI, 
            null, 
            Data.HAS_PHONE_NUMBER + "!=0 AND (" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?)", 
            new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
            Data.CONTACT_ID);
    
    while (c.moveToNext()) {
        long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID));
        String name = c.getString(c.getColumnIndex(Data.DISPLAY_NAME));
        String data1 = c.getString(c.getColumnIndex(Data.DATA1));
        
        System.out.println(id + ", name=" + name + ", data1=" + data1);
    }

科特林:

        var c : Cursor? = null
        try {
                val resolver : ContentResolver = this.contentResolver //Replace "this" With Whatever The Reference To Your Activity Is (If Need Be)
                c = resolver.query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.HAS_PHONE_NUMBER + "!=0 AND (" + ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + "=?)",
                    arrayOf(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE),
                    ContactsContract.Data.CONTACT_ID)

                if (c!!.count > 0) {
                    while (c.moveToNext()) {
                        val id : Long = c.getLong(c.getColumnIndex(ContactsContract.Data.CONTACT_ID))
                        val name : String = c.getString(c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
                        val data1 : String = c.getString(c.getColumnIndex(ContactsContract.Data.DATA1))

                        Log.d("Tag", "$id) name=$name, data1=$data1")
                    }
                }
        } finally {
            c!!.close()
        }

如果您的目标是 2.3,则需要考虑以下事实:

HAS_PHONE_NUMBER
无法通过查询
Data
时使用的联接来使用。

有趣

例如,可以通过跳过联系人必须有电话号码的要求来解决,而是选择“至少有电话号码或电子邮件地址的任何联系人”:

Cursor c = resolver.query(
        Data.CONTENT_URI, 
        null, 
        Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?", 
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
        Data.CONTACT_ID);

如果这不是一个选项,你总是可以进行可怕的hacky子选择:

Cursor c = resolver.query(
        Data.CONTENT_URI, 
        null, 
        "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?) AND " + 
        Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts WHERE " + Contacts.HAS_PHONE_NUMBER + "!=0)", 
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE}, Data.CONTACT_ID);

或使用 two

Cursor
s:

解决它
Cursor contacts = resolver.query(Contacts.CONTENT_URI, 
        null, Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID + " ASC");
Cursor data = resolver.query(Data.CONTENT_URI, null, 
        Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?", 
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE}, 
        Data.CONTACT_ID + " ASC");

int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
boolean hasData = data.moveToNext();

while (contacts.moveToNext()) {
    long id = contacts.getLong(idIndex);
    System.out.println("Contact(" + id + "): " + contacts.getString(nameIndex));
    if (hasData) {
        long cid = data.getLong(cidIndex);
        while (cid <= id && hasData) {
            if (cid == id) {
                System.out.println("\t(" + cid + "/" + id + ").data1:" + 
                        data.getString(data1Index));
            }
            hasData = data.moveToNext();
            if (hasData) {
                cid = data.getLong(cidIndex);
            }
        }
    }
}

5
投票

我遇到了完全相同的问题。从那时起,我构建了自己的解决方案,该解决方案受到这篇文章的启发,但又有点不同。现在我想将其作为我的第一个 StackOverFlow 答案来分享:-)

它与 Jens 建议的双光标方法非常相似。这个想法是

1- 从联系人表中获取相关联系人
2-获取相关联系人信息(邮件、电话...)
3-合并这些结果

“相关”当然取决于你,但重要的是性能! 此外,我确信使用合适的 SQL 查询的其他解决方案也可以完成这项工作,但在这里我只想使用 Android ContentProvider 这是代码:

一些常数

public static String CONTACT_ID_URI = ContactsContract.Contacts._ID;
public static String DATA_CONTACT_ID_URI = ContactsContract.Data.CONTACT_ID;
public static String MIMETYPE_URI = ContactsContract.Data.MIMETYPE;
public static String EMAIL_URI = ContactsContract.CommonDataKinds.Email.DATA;
public static String PHONE_URI = ContactsContract.CommonDataKinds.Phone.DATA;
public static String NAME_URI = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) ? ContactsContract.Data.DISPLAY_NAME_PRIMARY : ContactsContract.Data.DISPLAY_NAME;
public static String PICTURE_URI = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts.PHOTO_ID;

public static String MAIL_TYPE = ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE;
public static String PHONE_TYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;

1 联系方式

这里我要求联系人的DISPLAY_NAME必须不含“@”,并且他们的信息与给定的字符串匹配(这些要求当然可以修改)。以下方法的结果是第一个光标:

public Cursor getContactCursor(String stringQuery, String sortOrder) {

    Logger.i(TAG, "+++++++++++++++++++++++++++++++++++++++++++++++++++");
    Logger.e(TAG, "ContactCursor search has started...");

    Long t0 = System.currentTimeMillis();

    Uri CONTENT_URI;

    if (stringQuery == null)
        CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    else
        CONTENT_URI = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(stringQuery));

    String[] PROJECTION = new String[]{
            CONTACT_ID_URI,
            NAME_URI,
            PICTURE_URI
    };

    String SELECTION = NAME_URI + " NOT LIKE ?";
    String[] SELECTION_ARGS = new String[]{"%" + "@" + "%"};

    Cursor cursor = sContext.getContentResolver().query(CONTENT_URI, PROJECTION, SELECTION, SELECTION_ARGS, sortOrder);

    Long t1 = System.currentTimeMillis();

    Logger.e(TAG, "ContactCursor finished in " + (t1 - t0) / 1000 + " secs");
    Logger.e(TAG, "ContactCursor found " + cursor.getCount() + " contacts");
    Logger.i(TAG, "+++++++++++++++++++++++++++++++++++++++++++++++++++");

    return cursor;
}

正如您所见,这个查询的性能非常好!

2 联系方式

现在让我们获取联系信息。此时,我不在已获取的联系人和检索到的信息之间建立任何链接:我只是从数据表中获取所有信息...但是,为了避免无用的信息,我仍然要求 DISPLAY_NAMES 不含“@”,因为我'我对电子邮件和电话感兴趣,我要求数据 MIMETYPE 为 MAIL_TYPE 或 PHONE_TYPE(请参阅常量)。这是代码:

public Cursor getContactDetailsCursor() {

    Logger.i(TAG, "+++++++++++++++++++++++++++++++++++++++++++++++++++");
    Logger.e(TAG, "ContactDetailsCursor search has started...");

    Long t0 = System.currentTimeMillis();

    String[] PROJECTION = new String[]{
            DATA_CONTACT_ID_URI,
            MIMETYPE_URI,
            EMAIL_URI,
            PHONE_URI
    };

    String SELECTION = ContactManager.NAME_URI + " NOT LIKE ?" + " AND " + "(" + MIMETYPE_URI + "=? " + " OR " + MIMETYPE_URI + "=? " + ")";

    String[] SELECTION_ARGS = new String[]{"%" + "@" + "%", ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};

    Cursor cursor = sContext.getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            PROJECTION,
            SELECTION,
            SELECTION_ARGS,
            null);

    Long t1 = System.currentTimeMillis();

    Logger.e(TAG, "ContactDetailsCursor finished in " + (t1 - t0) / 1000 + " secs");
    Logger.e(TAG, "ContactDetailsCursor found " + cursor.getCount() + " contacts");
    Logger.i(TAG, "+++++++++++++++++++++++++++++++++++++++++++++++++++");

    return cursor;
}

您将再次看到这个查询非常快...

3 组合

现在让我们合并联系人及其各自的信息。这个想法是使用 HashMap(Key, String),其中 Key 是联系人 ID,String 是您喜欢的任何内容(姓名、电子邮件等)。

首先,我运行联系人光标(按字母顺序排列)并将名称和图片 uri 存储在两个不同的 HashMap 中。另请注意,我将所有联系人 ID 存储在列表中,其顺序与联系人在光标中出现的顺序完全相同。我们将此列表称为 contactListId

我对联系信息(邮件和电子邮件)执行相同的操作。但现在我要处理两个光标之间的关联:如果电子邮件或电话的 CONTACT_ID 没有出现在 contactListId 中,则会将其放在一边。我还会检查是否已遇到该电子邮件。请注意,进一步的选择可能会在名称/图片内容和电子邮件/电话哈希映射内容之间引入不对称,但不用担心。

最终,我遍历 contactListId 列表并构建一个 Contact 对象列表,注意以下事实:联系人必须有信息(keySet 条件)并且联系人必须至少有一封邮件或电子邮件(邮件的情况)例如,如果联系人是 Skype 联系人,则可能会出现 == null && Phone == null)。 这是代码:

public List<Contact> getDetailedContactList(String queryString) {

    /**
     * First we fetch the contacts name and picture uri in alphabetical order for
     * display purpose and store these data in HashMap.
     */

    Cursor contactCursor = getContactCursor(queryString, NAME_URI);

    List<Integer> contactIds = new ArrayList<>();

    if (contactCursor.moveToFirst()) {
        do {
            contactIds.add(contactCursor.getInt(contactCursor.getColumnIndex(CONTACT_ID_URI)));
        } while (contactCursor.moveToNext());
    }

    HashMap<Integer, String> nameMap = new HashMap<>();
    HashMap<Integer, String> pictureMap = new HashMap<>();

    int idIdx = contactCursor.getColumnIndex(CONTACT_ID_URI);

    int nameIdx = contactCursor.getColumnIndex(NAME_URI);
    int pictureIdx = contactCursor.getColumnIndex(PICTURE_URI);

    if (contactCursor.moveToFirst()) {
        do {
            nameMap.put(contactCursor.getInt(idIdx), contactCursor.getString(nameIdx));
            pictureMap.put(contactCursor.getInt(idIdx), contactCursor.getString(pictureIdx));
        } while (contactCursor.moveToNext());
    }

    /**
     * Then we get the remaining contact information. Here email and phone
     */

    Cursor detailsCursor = getContactDetailsCursor();

    HashMap<Integer, String> emailMap = new HashMap<>();
    HashMap<Integer, String> phoneMap = new HashMap<>();

    idIdx = detailsCursor.getColumnIndex(DATA_CONTACT_ID_URI);
    int mimeIdx = detailsCursor.getColumnIndex(MIMETYPE_URI);
    int mailIdx = detailsCursor.getColumnIndex(EMAIL_URI);
    int phoneIdx = detailsCursor.getColumnIndex(PHONE_URI);

    String mailString;
    String phoneString;

    if (detailsCursor.moveToFirst()) {
        do {

            /**
             * We forget all details which are not correlated with the contact list
             */

            if (!contactIds.contains(detailsCursor.getInt(idIdx))) {
                continue;
            }

            if(detailsCursor.getString(mimeIdx).equals(MAIL_TYPE)){
                mailString = detailsCursor.getString(mailIdx);

                /**
                 * We remove all double contact having the same email address
                 */

                if(!emailMap.containsValue(mailString.toLowerCase()))
                    emailMap.put(detailsCursor.getInt(idIdx), mailString.toLowerCase());

            } else {
                phoneString = detailsCursor.getString(phoneIdx);
                phoneMap.put(detailsCursor.getInt(idIdx), phoneString);
            }

        } while (detailsCursor.moveToNext());
    }

    contactCursor.close();
    detailsCursor.close();

    /**
     * Finally the contact list is build up
     */

    List<Contact> contacts = new ArrayList<>();

    Set<Integer> detailsKeySet = emailMap.keySet();

    for (Integer key : contactIds) {

        if(!detailsKeySet.contains(key) || (emailMap.get(key) == null && phoneMap.get(key) == null))
            continue;

        contacts.add(new Contact(String.valueOf(key), pictureMap.get(key), nameMap.get(key), emailMap.get(key), phoneMap.get(key)));
    }

    return contacts;
}

联系人对象的定义由您决定。

希望这会有所帮助,并感谢之前的帖子。

修正/改进

我忘了检查手机按键设置:它应该看起来像

!mailKeySet.contains(key)

替换为

 (!mailKeySet.contains(key) && !phoneKeySet.contains(key))

用手机按键设置

Set<Integer> phoneKeySet = phoneMap.keySet();

我为什么不添加一个空的联系人光标检查,例如:

if(contactCursor.getCount() == 0){
        contactCursor.close();
        return new ArrayList<>();
    }

在 getContactCursor 调用之后

© www.soinside.com 2019 - 2024. All rights reserved.