Search code examples
androidandroid-contentprovidercontactsandroid-contacts

Android phone lookup (PhoneLookup.CONTENT_FILTER_URI) returns the same contact multiple times


I am looking up a contact using a phone number as described here

Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(givenNumber));

final Account availableAccounts[] = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE);
Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI;

Cursor phoneContact = context.getContentResolver().query(
    lookupUri
    , null //new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME}
    ,null,null, null);

DatabaseUtils.dumpCursor(phoneContact);

while (phoneContact.moveToNext()) {
    long contactID = phoneContact.getLong(phoneContact.getColumnIndex(ContactsContract.PhoneLookup._ID));            
}
phoneContact.close();

The projection is set to null for debug purposes. I am getting the same contact multiple times as can be seen by the result of the DatabaseUtils.dumpCursor(phoneContact);:

>>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@1517c824
0 {
number=+00001122334
photo_uri=content://com.android.contacts/display_photo/6
send_to_voicemail=0
lookup=32i415560988b4aed0e.32i636352790bf3a3a6.3789r869-2D4531433931.3746i15
display_name=John Doe
last_time_contacted=1425383680707
has_phone_number=1
in_visible_group=1
photo_file_id=6
label=null
starred=0
normalized_number=+00001122334
photo_thumb_uri=content://com.android.contacts/contacts/212/photo
photo_id=1427
in_default_directory=1
custom_ringtone=null
_id=212
type=2
times_contacted=44
}
1 {
number=0001122334
photo_uri=content://com.android.contacts/display_photo/6
send_to_voicemail=0
lookup=32i415560988b4aed0e.32i636352790bf3a3a6.3789r869-2D4531433931.3746i15
display_name=John Doe
last_time_contacted=1425383680707
has_phone_number=1
in_visible_group=1
photo_file_id=6
label=null
starred=0
normalized_number=+27001122334
photo_thumb_uri=content://com.android.contacts/contacts/212/photo
photo_id=1427
in_default_directory=1
custom_ringtone=null
_id=212
type=2
times_contacted=44
}
2 {
number=0001122334
photo_uri=content://com.android.contacts/display_photo/6
send_to_voicemail=0
lookup=32i415560988b4aed0e.32i636352790bf3a3a6.3789r869-2D4531433931.3746i15
display_name=John Doe
last_time_contacted=1425383680707
has_phone_number=1
in_visible_group=1
photo_file_id=6
label=null
starred=0
normalized_number=+27001122334
photo_thumb_uri=content://com.android.contacts/contacts/212/photo
photo_id=1427
in_default_directory=1
custom_ringtone=null
_id=212
type=2
times_contacted=44
}
<<<<<

Note: I have replaced the phone number for privacy reasons. The only difference between the contacts is that the 1st one has the number in international format(+00), while the rest is in local format.

If anyone can please shed some light on this behaviour, a question identical to mine, but with less detail is here, but it is also unanswered. Similar questions consist of people not checking on mime_types, for example

Thank you.


Solution

  • See the answer here: https://stackoverflow.com/a/30249338/819355.

    Basically, you can use CommonDataKinds.Phone.CONTENT_FILTER_URI instead of PhoneLookup.CONTENT_FILTER_URI to get single results.