返回联系人电话号码的方法始终返回null - Android

时间:2017-05-02 13:34:44

标签: android android-contentprovider

请帮忙!我无法弄清楚为什么这种方法永远不会返回电话号码。我知道有些联系人可能没有电话号码,但总是会返回null。我正在为该方法提供我之前检索到的联系人ID。

    private String retrieveContactNumber(String id) {


    String contactNumber = "not set, null";

    Log.d(TAG, "Contact ID: " + id);

    Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                    ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

            new String[]{id},
            null);

    if (cursorPhone.moveToFirst()) {
        contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

    cursorPhone.close();

    Log.d(TAG, "Contact Phone Number: " + contactNumber);

    return contactNumber;
}

0 个答案:

没有答案