Android ContactsContract.Contacts 慢
我想将手机中的所有联系人放入一个数组中。我正在使用下面的代码,但将它们全部放入数组需要 3-4 秒。为了加快这一过程,我只收集那些拥有电话号码的联系人。这会产生一个包含 163 个元素的数组。
final String[] projection1 = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection1, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactnumbers.add(number);
}
pCur.close();
}
}
}
为什么这么慢?
I want to put all the contacts in my phone into an array. I am using the code below but it takes 3-4 seconds to put all of them into the array. To accelerate the process, I am collecting only those contacts who has a phone number. This results in an array of 163 elements.
final String[] projection1 = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection1, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactnumbers.add(number);
}
pCur.close();
}
}
}
Why is this so slow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在查询中有一个查询。为什么不创建一个连接查询来连接两个语句。这可能比当前的解决方案快得多。
You have a query inside a query. Why don't you create a joined query where you join both statements. This will be probably much faster than the current solution.