我想从安卓设备上检索>10000个联系人。需要8-10分钟才能联系到这么多人。还有其他可能的方法吗。我已经实现了一种方法--它工作得很好--但是当涉及到大量的联系人时,它需要花费时间来获取联系人。
ContentResolver cr = getActivity().getApplication().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
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()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = phoneNumber.replace(" ","");
phoneNumber = phoneNumber.replace("-","");
boolean addNumber = stringCheck(phoneNumber,symbols);
if (!addNumber){
if (phoneNumber.length() == 10){
addContact(phoneNumber,phoneType,name);
}else if (phoneNumber.length() == 11){
phoneNumber = phoneNumber.substring(1);
addContact(phoneNumber,phoneType,name);
}else if (phoneNumber.length() == 12){
phoneNumber = phoneNumber.substring(2);
addContact(phoneNumber,phoneType,name);
}else if (phoneNumber.length() == 13){
phoneNumber = phoneNumber.substring(3);
addContact(phoneNumber,phoneType,name);
}
}
}
pCur.close();
}
}
}发布于 2016-12-25 15:16:46
如果900在1000联系人中有电话号码,您目前正在查询DB 901次数,则可以将其简化为,只需两个查询
然后使用contact-id字段将手机与正确的联系人进行匹配。
此外,正如在其他答案中所指出的,您确实应该将projection添加到所有查询中,以提高性能。
您可以做的另一个改进是避免运行时cur.getColumnIndex()调用,如果您有一个投影,您应该已经知道索引,所以使用它硬编码。
Map<Long, List<String>> phones = new HashMap<>();
ContentResolver cr = getActivity().getApplication().getContentResolver();
// First build a mapping: contact-id > list of phones
Cursor cur = cr.query(Phone.CONTENT_URI, new String[] { Phone.CONTACT_ID, Phone.Number }, null, null, null);
while (cur != null && cur.moveToNext()) {
long contactId = cur.getLong(0);
String phone = cur.getString(1);
List list;
if (phones.contains(contactId)) {
list = phones.get(contactId);
} else {
list = new ArrayList<String>();
phones.put(contactId, list);
}
list.add(phone);
}
cur.close();
// Next query for all contacts, and use the phones mapping
cur = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID, Contacts.DISPLAY_NAME }, null, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
List<String> contactPhones = phones.get(id);
addContact(id, name, contactPhones);
}发布于 2016-12-21 06:17:01
将您的获取过程保存在doInBackground方法的AyncTask中,然后显示它。并只获取这些信息,这是首先需要的,例如,联系名称和ID。
请参阅以下ans以获得更多澄清:Fetching a large number of contacts
发布于 2016-12-21 06:25:03
为了获取大量的联系人,您必须使用rest以页面的形式获取联系人。例如,联系人的页面大小为100。
一种可能的办法是:
如果在列表视图中显示联系人,则可以在用户滚动列表时获取数据。为此,您必须设置列表的阈值位置,即80,因此当用户在滚动时到达第80位时,您可以点击rest为下一页获取新的联系人并将这些联系人添加到列表视图中。
从您的评论中,我可以看到您需要所有的联系人来移动forward.For,这可以在应用程序启动时启动意图服务。此服务将获取所有联系人并存储它们。你可以根据你的需要使用它们。
https://stackoverflow.com/questions/41256023
复制相似问题