开发者

Android Contacts Content URI HAS_PHONE_NUMBER not working as explained in documentation, please guide

开发者 https://www.devze.com 2023-04-04 04:17 出处:网络
I am trying to run the following code. ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,

I am trying to run the following code.

  ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  String name = cur.getString(cur.getColumnIndex(Conta开发者_JAVA技巧ctsContract.Contacts.DISPLAY_NAME));
                  if (Integer.parseInt(cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor pCur = cr.query(
                               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                               null,
                               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                               new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                     }
                    pCur.close();
                }
            }
        }
    }

So basically I query Contacts.URI and then on the basis of returned data if HAS_PHONE_NUMBER >0 , I query Phone.URI.

When I run this on my phone, I see 3 contacts that get displayed, who don't have corresponding entry in PHONE content URI. These 3 contacts have phone number for sure, as I can see those phone numbers when I go to my phone dialer screen.

How this is possible for a contact to have HAS_PHONE_NUMBER > 0 and still not have corresponding row in PHONE Content URI. Is it not weired, and how to get rid of this.

Please let me know. Thanks.


This small method gets the contacts based on an option string search parameter. It only fetches contacts that include phone numbers.

private Cursor getContactsCursor(String searchPattern){
    String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? and " + ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 ";
    String queryArguments = "%" + searchPattern + "%";
    String[] selectionArgs = new String[] { queryArguments };
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, selectionArgs, sortOrder);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消