如何有效地获取本地电话联系人
Posted
技术标签:
【中文标题】如何有效地获取本地电话联系人【英文标题】:how to fetch local phone contacts effeciently 【发布时间】:2016-06-19 09:27:00 【问题描述】:我正在使用以下代码来获取本地电话联系人。它工作正常,并且可以非常快速地获取联系人。
但是问题来了,在我的联系人列表中,很少有联系人拥有多个电子邮件和多个电话号码。
如果有多个电话或电子邮件地址,它会多次重复同一个人的姓名。
如果我改变
ContactsContract.CommonDataKinds.Email.CONTENT_URI
到ContactsContract.CommonDataKinds.Phone.CONTENT_URI
然后它会根据联系人存在的电话号码重复姓名。请帮忙
private static final String[] PROJECTION = new String[]
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.CommonDataKinds.Nickname.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Phone.NUMBER,
;
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null)
try
final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
final int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
long contactId;
String displayName, email, phone, photo;
while (cursor.moveToNext())
mNK_UserModel = new NK_Contact();
contactId = cursor.getLong(contactIdIndex);
displayName = cursor.getString(displayNameIndex);
//Adding display name
mNK_UserModel.setFirstName(displayName);
Util.DEBUG_LOG(1, "contact", "contact id :" + contactId);
al_PhoneContacts.add(mNK_UserModel);
finally
cursor.close();
【问题讨论】:
【参考方案1】:如果我不得不猜测,我会说您的 while 循环中缺少一个“中断”。因为游标试图获取下一个可用的列条目。但是看看我过去对我有用的解决方案。
它为每一行使用单独的游标,让您可以更好地控制数据。
Map<String, String> contactDataMap = new HashMap<String, String>();
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
contactDataMap.put(NAME, (name != null)?name:"");
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]id,
null);
while (pCur.moveToNext())
String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactDataMap.put(PHONE, (number != null)?number:"");
break;
pCur.close();
Cursor emailCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]id, null);
while (emailCur.moveToNext())
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contactDataMap.put(MAIL, (email != null)?email:"");
break;
emailCur.close();
cursor.close();
【讨论】:
以上是关于如何有效地获取本地电话联系人的主要内容,如果未能解决你的问题,请参考以下文章