无法在 Api 5.0(Lollipop & Marshmallow)及更高版本上获取 Phone 联系人

Unable to fetch Phone contact on and above Api 5.0 (Lollipop & Marshmallow)

我正在尝试从联系人列表中获取电子邮件和显示名称。我的代码在 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;
    }

这段代码适合我。

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

Need to add this permission into your manifest.

<uses-permission android:name="android.permission.READ_CONTACTS" />