【问题标题】:Unable to fetch Phone contact on and above Api 5.0 (Lollipop & Marshmallow) [closed]无法在 Api 5.0(棒棒糖和棉花糖)及更高版本上获取电话联系人 [关闭]
【发布时间】:2016-05-18 06:53:21
【问题描述】:

我正在尝试从联系人列表中获取电子邮件和显示名称。我的代码在 api 级别 4.4 (Kitkat) 之前运行良好,但在 5.0 Lollipop 或更高版本 (6.0 Marshmallow) 上运行良好。

这是我的代码:

public ArrayList<AddressData> getEmailDetails(){
        ArrayList<AddressData> alAdressBookData = new ArrayList<AddressData>();
        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));
                Cursor cur1 = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                new String[]{id}, null);
                while (cur1.moveToNext()) {
                    String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    AddressData obAddressData=new AddressData();
                    obAddressData.email=email;
                    obAddressData.name=name;
                    System.out.println("Contact Emails : "+ email);
                    System.out.println("Contact name : "+ name);
                    if(email!=null){
                        alAdressBookData.add(obAddressData);
                    }
                } 
                cur1.close();
            }
        }
        return alAdressBookData;
    }

【问题讨论】:

  • 什么不工作?任何异常?
  • 您可能应该从了解 API 和表开始,然后使用它来确定数据是如何存储的,并使用日志记录来确定您所看到的确切内容(也可以查看日志,同时尝试获取数据并查看系统的响应。学习您的 API 并调试您的代码,而不是依赖于它以前工作的事实。也应该从阅读文档开始,因为 android M 的权限结构已更改,这可能是您的问题。您可能有 2 个问题,棒棒糖和 M,看到的问题可能不是因为相同的原因。

标签: android android-contacts android-contentresolver


【解决方案1】:

这段代码对我来说很好。

public static List<PhoneContact> getPhoneBook(Context context) {
    List<PhoneContact> result = new ArrayList<>();
    ContentResolver resolver = context.getContentResolver();
    Cursor contacts = null;
    try {
        contacts = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (contacts.moveToFirst()) {
            do {
                String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));
                PhoneContact phoneContact = new PhoneContact();
                Cursor emails = null;
                Cursor phones = null;
                try {
                    emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = " + contactId, null, null);
                    while (emails.moveToNext()) {
                        String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        // Add email to your phoneContact object
                    }
                    phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                    while (phones.moveToNext()) {
                        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String displayName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        // Add others information into your phoneContact object
                    }
                } finally {
                    if (emails != null) {
                        emails.close();
                    }
                    if (phones != null) {
                        phones.close();
                    }
                }
                result.add(phoneContact);
            } while (contacts.moveToNext());
        }
    } finally {
        if (contacts != null) {
            contacts.close();
        }
    }
    return result;
}

需要将此权限添加到您的清单中。

&lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;

【讨论】:

  • 感谢您的回答。它正在 Lolipop 5.0 上运行,但如何获取 电子邮件地址
  • 很高兴它成功了。
猜你喜欢
  • 2016-09-26
  • 1970-01-01
  • 2016-05-02
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 2017-01-28
相关资源
最近更新 更多