从手机读取通讯录非常慢

时间:2019-01-23 10:59:03

标签: android performance cursor android-contacts import-contacts

我正在尝试从手机中获取所有联系人,包括从具有多个号码的联系人中获取所有号码。

因此,我建立了一个查询,该查询不会在整个联系人中都运行过度,并建立了Contact用户,并且具有带有ID选择的内部查询来获取每个用户的所有号码。但是由于我的内部查询包括选择,因此需要很长时间。还有其他想法吗?

private Cursor initPhoneCursor() {
    try {

        // get the contacts URI
        final Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        // get the name column's name depending on the Android Version
        final String nameColumn = Contact.COLUMN_NAME_PHONE;


        // declare columns object - init later depending on version
        String selection = getQuerySelectionForCursor();

        String[] columns = getColumnSelectionForCursor(nameColumn);

        if (mApp != null) {
            // return cursor from contentresolver
            return mApp.getContentResolver().query(phoneUri, columns, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        }
    } catch (Exception e) {
        // couldn't read phone cursor

        CaughtExceptionHandler.reportException(e);
    }
    return null;
}

private void importContactsFromCursor(Cursor cursor, boolean isSimCard) {


    mCurrentContactCursor = initPhoneCursor();

    // check cursor is alive
    if (cursor != null && !cursor.isClosed()) {

        while (cursor.moveToNext() && shouldContinueImport()) {

//                // as log as we have contacts, move through them
            importContact(cursor, isSimCard);

            mCurrentContact++;

        }
        // when done - close the cursor
        cursor.close();
    }
}

private void importContact(Cursor cursor, boolean simCard) {

    // create Contact object
    Contact row = new Contact(cursor, simCard);

//        mContactsTimer.onContactCreated();


    if (simCard) {
        // if simCard, contact must have number

        // validate number and create contact
        row = validateAndCheckNumber(row, cursor);

    }
    else {
        // if not sim card (phone cursor), a contact might have no numbers,
        // single or multiple phone numberss

        // let's check if this contact has any numbers
        if (hasPhoneNumbers(cursor)) {

            // get all of the contact's phone numbers
            row = importAllNumbersForContact(row);

        }

    }

    // check if this is valid
    final boolean isValidForSaving = row != null && row.hasName() && row.hasNumbers();

    if (isValidForSaving && !sStopRequested) {

        mContactsToSave.add(row);
    }

}

private Contact importAllNumbersForContact(Contact contact) {

    // uri of contact phones
    Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    // contact_id = ?
    String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
    String[] selectionArgs = {String.valueOf(contact.getOriginalId())};

    // do the query
    Cursor phoneCursor = mApp.getContentResolver().query(contentUri, null, selection, selectionArgs, null);

    if (phoneCursor != null) {

        // save numbers if we got anything
        contact = loopThroughContactNumbers(contact, phoneCursor);


        // close cursor when done
        phoneCursor.close();

    }
    return contact;
}

1 个答案:

答案 0 :(得分:0)

采用以下解决方案:

Map<String,Contact> contactsMap = new TreeMap<>();
    contacts = new ArrayList<>();

    Cursor phones = getBaseContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
    assert phones != null;
    while (phones.moveToNext())
    {
        Contact contact = new Contact();
        contact.setDisplayName(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
        contact.setPhoneNumber(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        contact.setDisplayPicture(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI)));
        contactsMap.put(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),contact);
    }

    contacts.addAll(contactsMap.values());
    phones.close();

为联系人的所有号码进行修改。你很好。