gpt4 book ai didi

android - 从手机读取联系人非常慢

转载 作者:行者123 更新时间:2023-11-30 05:07:28 27 4
gpt4 key购买 nike

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

因此,我构建了一个查询,在不过度运行所有联系人的同时,构建了联系用户,并在内部查询中选择了 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;
}

最佳答案

采用以下解决方案:

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();

修改一个联系人的所有号码。你很适合一起去。

关于android - 从手机读取联系人非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54325679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com