从联系人 ID 获取联系人详细信息在 android 中不起作用

Getting contactdetails from contact Id not working in android

你好,我正在处理联系人目录,我制作了自定义通话记录,不,我想在单击其中一个联系人时显示联系人详细信息,我首先从联系人中获取 contact_id号码,然后通过互联网搜索,我得到了一个从联系人 ID 获取所有详细信息的功能,我已经把它放在我的代码中,但它不起作用,没有给我结果,

public void getDetials(String contactID) {
        Uri myPhoneUri = Uri.withAppendedPath(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                Uri.encode(contactID));

        // Query the table
        @SuppressWarnings("deprecation")
        Cursor phoneCursor = managedQuery(myPhoneUri, null, null, null, null);

        // Get the phone numbers from the contact
        for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor
                .moveToNext()) {

            // Get a phone number
            String phoneNumber = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            //get name
            String phonename = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));




            //get email
            String phoneemail = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));

            //get image uri..!!
            String img_uri = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            System.out
                    .println("=============my phone number ,is==================="
                            + phoneNumber + "======in call info=="+"\n =========name is===="+phonename+"=================email is========"+phoneemail);
            System.out.println("======myimgae url fopr the contact is============="+img_uri);

        }

    }

这是读取指定联系人的所有 phone 号码和电子邮件的方法:

public void getContactDetails(int contactId) {
    Log.d("Details", "---");
    Log.d("Details", "Contact : " + contactId);
    final Cursor phoneCursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
            },
            Data.CONTACT_ID + "=?",
            new String[] {String.valueOf(contactId)}, null);

    try {
        final int idxAvatarUri = phoneCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Phone.PHOTO_URI);
        final int idxName = phoneCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        final int idxPhone = phoneCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Phone.NUMBER);

        while (phoneCursor.moveToNext()) {
            String phoneNumber = phoneCursor.getString(idxPhone);
            String name = phoneCursor.getString(idxName);
            String avatarUri = phoneCursor.getString(idxAvatarUri);

            Log.d("Details", "Phone number: " + phoneNumber);
            Log.d("Details", "Name: " + name);
            Log.d("Details", "Avatar URI: " + avatarUri);
        }
    } finally {
        phoneCursor.close();
    }

    final Cursor emailCursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Email.ADDRESS,
            },
            Data.CONTACT_ID + "=?",
            new String[] {String.valueOf(contactId)}, null);

    try {
        final int idxAddress = emailCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Email.ADDRESS);
        while (emailCursor.moveToNext()) {
            String address = emailCursor.getString(idxAddress);
            Log.d("Details", "Email: " + address);
        }
    } finally {
        emailCursor.close();
    }
}

请注意,一个联系人可以持有多个 phone 号码和电子邮件地址。

以下是获取所有联系人详细信息的方法:

final Cursor cur = getContentResolver().query(Data.CONTENT_URI,
        new String[]{Data.CONTACT_ID}, null, null, null);
try {
    final int idxId = cur.getColumnIndex(Data.CONTACT_ID);
    while (cur.moveToNext()) {
        final int id = cur.getInt(idxId);
        getContactDetails(id);
    }
} finally {
    cur.close();
}

不要忘记添加权限:

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