android - 提高使用光标获取联系人的速度
问题描述
在我的应用程序中,我正在使用光标获取所有联系人。
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 ContactsContract API 时,这是一个非常常见的问题,您基本上是在执行一个大查询来获取所有联系人,然后执行大量小查询来获取电话和电子邮件。
因此,在有 1000 个联系人的手机上,您可能需要进行 2001 次查询才能获取所有信息。
相反,您可以通过一个查询来获取所需的所有信息,您只需跳过Contacts
表格并直接在Data
包含您需要的所有信息的表格上查询。
我不太明白你在用 , 和那些 / 做什么contact_id
,name
所以MAIL_TAG
我UPN_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 线程上运行它。
推荐阅读
- java - 按下按钮后如何开始循环功能
- r - 替换 sqldf 中的字符串模式
- tomcat - Tomcat Catalina 输出日志轮换并追加新行
- python - mongoengine.connect - 如何连接到另一个非默认端口?
- angular - NullInjectorError: No provider for /config.json!” 我可以将变量作为依赖项注入吗?
- r - 使用 group_by 变量创建 group_by 和 count 表的问题
- linux - 从 git repo 拉入生产服务器的良好实践
- java - Intellij 中的语法高亮 - 自定义语言
- sas - 如何在 SAS 中的 slicefit 绘图选项中修复 x 轴的范围
- java - 检查客户端机器上是否有cookie,否则你做一些事情并退出