[英]Fast reading contacts android
在Android中有没有更快的方法来阅读联系人? 例如,使用光标的方法需要3-5秒才能读取30-50个联系人。 这很长。
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
names.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
numbers.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
}
}
有任何想法吗?
你的问题很有趣......
阅读快速联系人,因为从ContactsContract读取联系人数据需要时间。
我不知道你使用的另一种方式,但你仍然可以通过为managedQuery提供String []投影参数来提高性能...
仅从contactsContract中获取您需要的数据,因为通过提供空值,它将获取记录的所有列。
对不起,我的英语不好。 我使用HashMap创建了类似但不同的样式。 我会粘贴我的方法。
//Create a hashMap, Key => Raw Contact ID and value => Object of customClass
HashMap<Long, ContactStructure> mFinalHashMap = new HashMap<Long, ContactStructure>();
//Run IN query in data table. example
select mimetype_id, raw_contact_id, data1 to data14 from data where raw_contact_id IN (select _id from raw_contacts where deleted <> 1 and account_type = "phone" and account_name = "bla bla") and mimetype_id = (select _id from mimetypes where mimetype = "vnd.something.phone");
现在创建一个将包含所有联系数据的类。
在访问光标时。
while (cursor.moveToNext()) {
ContactStructure contactStructure = mFinalHashMap.get(rawContactID);
//It will return the previous instance of object, If we already put
if(rawContactStructure == null) {
contactStructure = ContactStructure.provideInstance();
}
//Now check for your required mimeType
case MIMETYPE_PHONE:
contactStructure.hasPhoneNo = true;
contactStructure.phoneNumbers.add(addDetail); //add the data1 .. to data14
break;
}
/*Demo class for saving the details*/
public class ContactMetaData {
static classContactStructure {
boolean hasPhoneNo;
List<List<String>> phoneNumbers;
public static ContactStructure provideInstance() {
contact.phoneNumbers = new ArrayList<List<String>>();
ContactStructure contact = new RawContactStructure();
return contact
}
}
使用这种方法,我尝试了3000个联系人的所有数据,它很快。 因为它不容易得到所有的联系人和他们的所有数据与秒..
要更快地阅读联系人,您需要使用投影的概念,其中您只需指定要获取的列。
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
int contactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Log.d("con ", "name " + contactName + " " + " PhoeContactID " + phoneContactID + " ContactID " + contactID)
cursor.moveToNext();
}
这将帮助您将完成教程的时间缩短90%我使用此站点http://www.blazin.in/2016/02/loading-contacts-fast-from-android.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.