开发者

ContactPicker filtered

开发者 https://www.devze.com 2023-04-06 04:11 出处:网络
In a simple Android App I need to get phone number from Address Book using Contacts picker. To open Contact picker I use following code:

In a simple Android App I need to get phone number from Address Book using Contacts picker.

To open Contact picker I use following code:

            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);

All works fine but in Contacts Picker I can see contacts from gmail, if I use Contact Picker in Phone App I see only Addess book's contacts. How can I sho开发者_运维技巧w a Contact Picker as Phone App (without gmail contacts)?


This helped me Filter just for phone numbers:

Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
        ArrayList<String> phoneNumber = new ArrayList<String>();
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));

            String name = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            phoneNumber.add(name);
            String hasPhone = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if (hasPhone.equalsIgnoreCase("1")) {
                hasPhone = "true";
            } else {
                hasPhone = "false";
            }

            if (Boolean.parseBoolean(hasPhone)) {
                Cursor phones = null;

                phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);

                while (phones.moveToNext()) {

                    phoneNumber
                            .add(phones.getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

                }
                phones.close();
            }
        }
        cursor.close();
0

精彩评论

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

关注公众号