Android SDK-获取联系人非常缓慢
作者:互联网
一个新的应用程序,我需要使用手机导入所有联系人,
我在AsyncTask类中运行以下代码.
在具有2000个触点的设备上,它工作正常,但速度非常慢,该设备冻结了片刻.
我知道它可以更快地完成,因为有很多使用联系人的应用程序.
有任何想法吗?
public ArrayList<ContactInfo> getContacts() {
ArrayList<ContactInfo> arrayList = new ArrayList<ContactInfo>();
ContentResolver cr = GlobalData.instance().getContext().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
String id;
String name;
int counter = 0;
if (cur.getCount() > 0) {
int indexId= cur.getColumnIndex(ContactsContract.Contacts._ID);
int indexName = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int indexHasPhoneNum = cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
Log.d("getContacts", "Start");
while (cur.moveToNext()) {
id = cur.getString(indexId);
name = cur.getString(indexName);
if (Integer.parseInt(cur.getString(indexHasPhoneNum)) > 0) {
// Query phone here. Covered next
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext()) {
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
if(type == Phone.TYPE_MOBILE){
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
arrayList.add(new ContactInfo(name, phoneNumber));
counter++;
}
}
phones.close();
}
}
Log.d("getContacts", "End (" + counter + ")" );
}
cur.close();
return arrayList;
解决方法:
在使用其他资源和一些常识进行搜索之后,使所有手机都进入设备的答案是查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI而不是ContactsContract.Data.CONTENT_URI并在其上运行光标.
标签:performance,import,contacts,android 来源: https://codeday.me/bug/20191029/1962183.html