PhoneLookup.CONTENT_FILTER_URI returns twice the same contact

跟風遠走 提交于 2019-12-08 05:22:50

问题


in my code below, i am trying to get all contacts with a specific phone number.

however it looks like i always get some of the contacts id more then once. specifically i have 2 contacts with the same phone number, and i get 3 contact Id's. one of them twice (the same ID)

any ideas?

thanks

Cursor contactLookupCursor =  
localContentResolver.query(
    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(requestedPhone)), 
                    new String[] {PhoneLookup._ID}, 
                    null, 
                    null, 
                    null);

if (contactLookupCursor != null)
{
    System.out.println("contactLookupCursor.getCount = "+contactLookupCursor.getCount()); // here i get 3
    if(contactLookupCursor.moveToFirst())
    {
        do
        {
            int ColumnIndex = contactLookupCursor.getColumnIndex(PhoneLookup._ID);
            if(ColumnIndex >= 0)
            {
                String contactId = contactLookupCursor.getString(ColumnIndex);
                System.out.println("contactId="+contactId);// here i get 12 then 13 then 13 again
            }
        }
        while (contactLookupCursor.moveToNext());
    }
    contactLookupCursor.close();
}

回答1:


If you provide a non-normalized phone number to PhoneLookup.CONTENT_FILTER_URI then it will be matched at two positions in the data record table of phone numbers.

1 match for number with column "number" in record.
1 match for normalized_number with column "normalized_number" in record.

If you provide number = "+49177123456" then number == normalized_number .

In this case PhoneLookup.CONTENT_FILTER_URI will only search in the "normalized_number" column of the phone table.


You can alternatively use CommonDataKinds.Phone instead of PhoneLookup.

This way you don't get duplicate results whether the provided phone number is normalized or not:

String number = "0177123456";
Uri uri =     Uri.withAppendedPath(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,Uri.encode(number));
Cursor rcursor = getContentResolver().query(uri,
                                        new String[] { ContactsContract.CommonDataKinds.Phone.CONTACT_ID },
                                        null,null,null);

I guess the strange behavior of PhoneLookup is a bug .

But maybe PhoneLookup is more performant than CommonDataKinds.Phone:

Class Overview: [...] This query is highly optimized. http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

There is no such statement for CommonDataKinds.Phone.



来源:https://stackoverflow.com/questions/21967727/phonelookup-content-filter-uri-returns-twice-the-same-contact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!