0

I got the way to check if a contact has a phone number by using
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)
    {

    }
}

Similary is der a way to check email ? like
HAS_EMAIL

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

}
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
  • Refer this http://stackoverflow.com/questions/10117049/get-only-email-address-from-contact-list-android#10117750 – Indra Kumar S May 15 '17 at 18:47
  • Yes I did in that way. But I felt, if I check before email query, using something like HAS_EMAIL, it wil decrease number of query and wil be faster. – Sujay U N May 15 '17 at 18:51
  • you can do it faster then the accepted answer in http://stackoverflow.com/questions/10117049/get-only-email-address-from-contact-list-android#10117750 in just one additional query instead of one query per contact, is that what you're looking for? – marmor May 16 '17 at 13:58
  • Yes exactly., Need that as a coloumn index of query – Sujay U N May 17 '17 at 21:38

1 Answers1

2

As a workaround, you can run a single query to get all the contact-ids that have emails, store those ids in a set, and use that as an "HAS_EMAIL" reference:

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
}
marmor
  • 27,641
  • 11
  • 107
  • 150