PhoneLookup.CONTENT_FILTER_URI两次返回同一联系人两次、联系人、PhoneLookup、CONTENT_FILTER_URI

2023-09-06 08:53:26 作者:我請客你買單

在我下面的code,我试图得到一个特定的电话号码的所有联系人。

但它看起来像我总是得到一些接触ID的更多然后一次。特别是我有同一个电话号码2的接触,我也得到3接触式ID的。他们中的一个两次(相同的ID)

什么想法?

感谢

 光标contactLookupCursor =localContentResolver.query(    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.en code(requestedPhone))                    新的String [] {} PhoneLookup._ID,                    空值,                    空值,                    空值);如果(contactLookupCursor!= NULL){    的System.out.println(contactLookupCursor.getCount =+ contactLookupCursor.getCount()); //这里我得到3    如果(contactLookupCursor.moveToFirst())    {        做        {            INT参数:columnIndex = contactLookupCursor.getColumnIndex(PhoneLookup._ID);            如果(参数:columnIndex> = 0)            {                字符串的ContactID = contactLookupCursor.getString(参数:columnIndex);                的System.out.println(的ContactID =+的ContactID); //这里我得到12然后13,然后再13            }        }        而(contactLookupCursor.moveToNext());    }    contactLookupCursor.close();} 

解决方案 风险行为知多少 你有被智能手机所窥探么

如果您提供的非规范化的电话号PhoneLookup.CONTENT_FILTER_URI那么这将是在两个位置匹配的电话号码的数据记录表。

  1匹配在记录列号码号码。1匹配在记录列normalized_numbernormalized_number。 

如果您提供的数=49177123456和若干== normalized_number

在这种情况下 PhoneLookup.CONTENT_FILTER_URI将搜索仅在normalized_number列 电话表。

您也可以使用 CommonDataKinds.Phone 而不是PhoneLookup。

这样,你没有得到重复的结果是否提供的电话号码是归与否:

 串号=0177123456;URI URI = Uri.withAppendedPath(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,Uri.en$c$c(number));光标rcursor = getContentResolver()查询(URI,                                        新的String [] {} ContactsContract.CommonDataKinds.Phone.CONTACT_ID,                                        NULL,NULL,NULL); 

我猜PhoneLookup的奇怪的行为是一个错误。

但也许PhoneLookup更高性能比CommonDataKinds.Phone:

  

类概述:[...]这个查询是高度优化。的http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

有对CommonDataKinds.Phone没有这样的说法。

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();
}

解决方案

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.