首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ContactPicker已过滤

ContactPicker已过滤
EN

Stack Overflow用户
提问于 2011-09-21 18:07:42
回答 1查看 208关注 0票数 0

在一个简单的Android应用程序中,我需要从通讯录中使用联系人选择器获取电话号码。

要打开联系人选择器,我使用以下代码:

代码语言:javascript
复制
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);

一切正常,但在联系人Picker中我可以看到来自gmail的联系人,如果我在手机应用程序中使用Contact Picker,我只能看到Addess book的联系人。如何将联系人选择器显示为电话应用程序(没有gmail联系人)?

EN

回答 1

Stack Overflow用户

发布于 2011-09-21 18:44:14

这帮助我只过滤电话号码:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7498031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档