首页 > 解决方案 > 提高使用光标获取联系人的速度

问题描述

在我的应用程序中,我正在使用光标获取所有联系人。

private static final String[] PROJECTION = new String[]{
        PHONE_CONTACT_ID,
        DISPLAY_NAME,
        TIMESTAMP,
        HAS_PHONE_NUMBER};

CursorLoader cursorLoader = new CursorLoader(ResUtils.getInstance().getContext(),
            ContactsContract.Contacts.CONTENT_URI, PROJECTION, null, null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ")ASC");
    Cursor cursor = cursorLoader.loadInBackground();
    // Loop for every contact in the phone
    if (cursor != null && cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            ArrayList<String> phoneNumbers = new ArrayList<>();
            String contact_id = cursor.getString(cursor.getColumnIndex(PHONE_CONTACT_ID));
            String   name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
            String  timeStamp = cursor.getString(cursor.getColumnIndex(TIMESTAMP));
            cursorLoader = new CursorLoader( ResUtils.getInstance().getContext(), ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    new String[]{EMAIL},ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contact_id}, null);
            Cursor emailCur = cursorLoader.loadInBackground();
            while (emailCur.moveToNext()) {
                String email = emailCur.getString(emailCur.getColumnIndex(EMAIL));
                if (TmlyUtils.isValidEmail(email)) {
                    phoneNumbers.add(ContactUtils.MAIL_TAG + email);
                }
            }
            emailCur.close();
            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))) > 0) {
                cursorLoader = new CursorLoader( ResUtils.getInstance().getContext(),   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        new String[]{NUMBER},ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{contact_id}, null);
                Cursor phones = cursorLoader.loadInBackground();
                while (phones.moveToNext()) {
                    String phoneNumber = phones.getString(phones.getColumnIndex(NUMBER));
                    phoneNumber = isValidMobileNumber(phoneNumber);
                    if (!phoneNumber.isEmpty() && !phoneNumbers.contains(ContactUtils.UPN_TAG + phoneNumber)) {
                        phoneNumbers.add(ContactUtils.UPN_TAG + phoneNumber);
                    }
                }
                phones.close();
            }
        }
        cursor.close();
    }

该代码工作正常,但是当您有数千个联系人时,应用程序会在几秒钟内冻结。

我正在使用三个光标,第一个允许我获取手机中的所有联系人

CursorLoader cursorLoader = new CursorLoader(ResUtils.getInstance().getContext(),
        ContactsContract.Contacts.CONTENT_URI, PROJECTION, null, null,
        "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ")ASC");
Cursor cursor = cursorLoader.loadInBackground();

每个电子邮件地址的第二个循环

 cursorLoader = new CursorLoader( ResUtils.getInstance().getContext(), ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                new String[]{EMAIL},ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contact_id}, null);
        Cursor emailCur = cursorLoader.loadInBackground();

每个电话的第三个循环

cursorLoader = new CursorLoader( ResUtils.getInstance().getContext(),   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{NUMBER},ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{contact_id}, null);
            Cursor phones = cursorLoader.loadInBackground();

我需要使用第二个和第三个如果我不使用它我无法为每个联系人获取所有电话和电子邮件,因为一个联系人可以有多个电话和电子邮件地址

我尝试使用 CursorLoader 来加快速度,但这还不够,是否可以摆脱第二个和第三个光标?

编辑:我忘了说所有查询都已经在 Retrofit 调用中

标签: androidandroid-contactsandroid-cursorandroid-cursorloader

解决方案


在使用 Android ContactsContract API 时,这是一个非常常见的问题,您基本上是在执行一个大查询来获取所有联系人,然后执行大量小查询来获取电话和电子邮件。

因此,在有 1000 个联系人的手机上,您可能需要进行 2001 次查询才能获取所有信息。

相反,您可以通过一个查询来获取所需的所有信息,您只需跳过Contacts表格并直接在Data包含您需要的所有信息的表格上查询。

我不太明白你在用 , 和那些 / 做什么contact_idname所以MAIL_TAGUPN_TAG只会将你需要的信息打印到日志中,你需要将它分类到有意义的 java 对象中到您的应用程序。

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1};
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.moveToNext()) {
    long id = cur.getLong(0); // contact-id
    String name = cur.getString(1); // contact name
    String mime = cur.getString(2); // type: email / phone
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234

    switch (mime) {
        case Phone.CONTENT_ITEM_TYPE: 
            Log.i("Contacts", "got a phone: " + id + ", " + name + ", " + data);
            break;
        case Email.CONTENT_ITEM_TYPE: 
            Log.i("Contacts", "got an email: " + id + ", " + name + ", " + data);
            break;
    }
}
cur.close();

此外,正如@IliaKuzmin 所说,您应该在后台线程而不是 UI 线程上运行它。


推荐阅读