Android联系人检查联系人是否有电子邮件ID

时间:2017-05-15 18:44:00

标签: android email cursor contact contactscontract

我找到了通过使用
来检查联系人是否有电话号码的方法 HAS_PHONE_NUMBER

ContentResolver ContntRslverVar = getContentResolver();
Cursor ContctCorsorVar = ContntRslverVar.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while (ContctCorsorVar.moveToNext())
{

    if (Integer.parseInt(ContctCorsorVar.getString(ContctCorsorVar.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
    {

    }
}

类似的是检查电子邮件的方法吗?喜欢 HAS_EMAIL

if (Integer.parseInt(ContctCorsorVar.getString(ContctCorsorVar.getColumnIndex(ContactsContract.Contacts.HAS_EMAIL))) > 0)
{

}

1 个答案:

答案 0 :(得分:2)

作为一种变通方法,您可以运行单个查询来获取所有具有电子邮件的联系人ID,将这些ID存储在一个集合中,并将其用作" HAS_EMAIL"参考:

Set<Long> hasEmail = new HashSet<>();
// The Email class should be imported from CommonDataKinds.Email
Cursor cursor = getContentResolver().query(Email.CONTENT_URI, new String[] { Email.CONTACT_ID }, null, null, null); 
while (cursor != null && cursor.moveToNext()) {
    hasEmail.add(cursor.getLong(0));
}
if (cursor != null) {
    cursor.close();
}

// now you can check if a contact has an email via:
if (hasEmail.contains(someContactId)) {
    // do something
}

// or iterate over all contact-ids that has an email
Iterator<Long> it = hasEmail.iterator();
while(it.hasNext()) {
    Long contactId = it.next();
    // do something
}