【发布时间】:2020-06-02 08:40:11
【问题描述】:
在我的应用中,我使用光标获取所有联系人。
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 调用中了
【问题讨论】:
标签: android android-contacts android-cursor android-cursorloader