Android,无法访问联系人电话号码

时间:2013-02-18 16:15:35

标签: java android contacts android-contacts phone-number

我无法访问联系人的电话号码,也无法找出原因。

private void getContacts(){
        Cursor people = mCon.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (people.moveToFirst()) {
            do {
               int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
               String contact = people.getString(nameFieldColumnIndex).toLowerCase();
               int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
               String number = people.getString(numberFieldColumnIndex); 
               hm.put(contact, number);
            }while(people.moveToNext());
        }
        people.close();
    }

这些名称会很好地进入hashmap,但是numberFieldColumnIndex会一直显示为-1

我输出了人的列标题而没有一个数字?我该如何检索它们?

列出人物的列标题

02-18 13:26:38.500: D/Contacts(1587): 0 times_contacted
02-18 13:26:38.500: D/Contacts(1587): 1 contact_status
02-18 13:26:38.500: D/Contacts(1587): 2 phonetic_name
02-18 13:26:38.500: D/Contacts(1587): 3 phonetic_name_style
02-18 13:26:38.500: D/Contacts(1587): 4 link
02-18 13:26:38.500: D/Contacts(1587): 5 is_user_profile
02-18 13:26:38.500: D/Contacts(1587): 6 raw_contact_linkpriority4
02-18 13:26:38.500: D/Contacts(1587): 7 raw_contact_linkpriority5
02-18 13:26:38.500: D/Contacts(1587): 8 lookup
02-18 13:26:38.500: D/Contacts(1587): 9 contact_status_icon
02-18 13:26:38.500: D/Contacts(1587): 10 last_time_contacted
02-18 13:26:38.500: D/Contacts(1587): 11 sec_custom_vibration
02-18 13:26:38.500: D/Contacts(1587): 12 _id
02-18 13:26:38.500: D/Contacts(1587): 13 display_name_source
02-18 13:26:38.500: D/Contacts(1587): 14 photo_uri
02-18 13:26:38.500: D/Contacts(1587): 15 photo_thumb_uri
02-18 13:26:38.500: D/Contacts(1587): 16 contact_chat_capability
02-18 13:26:38.500: D/Contacts(1587): 17 photo_id
02-18 13:26:38.500: D/Contacts(1587): 18 send_to_voicemail
02-18 13:26:38.500: D/Contacts(1587): 19 display_name_reverse
02-18 13:26:38.500: D/Contacts(1587): 20 custom_ringtone
02-18 13:26:38.500: D/Contacts(1587): 21 name_raw_contact_id
02-18 13:26:38.500: D/Contacts(1587): 22 photo_file_id
02-18 13:26:38.500: D/Contacts(1587): 23 has_phone_number
02-18 13:26:38.500: D/Contacts(1587): 24 link_type5
02-18 13:26:38.500: D/Contacts(1587): 25 link_type4
02-18 13:26:38.500: D/Contacts(1587): 26 link_type3
02-18 13:26:38.500: D/Contacts(1587): 27 contact_status_label
02-18 13:26:38.500: D/Contacts(1587): 28 link_type2
02-18 13:26:38.500: D/Contacts(1587): 29 link_type1
02-18 13:26:38.500: D/Contacts(1587): 30 raw_contact_linkpriority3
02-18 13:26:38.500: D/Contacts(1587): 31 raw_contact_linkpriority2
02-18 13:26:38.500: D/Contacts(1587): 32 display_name
02-18 13:26:38.500: D/Contacts(1587): 33 raw_contact_linkpriority1
02-18 13:26:38.500: D/Contacts(1587): 34 has_email
02-18 13:26:38.500: D/Contacts(1587): 35 sort_key_alt
02-18 13:26:38.500: D/Contacts(1587): 36 dirty_contact
02-18 13:26:38.500: D/Contacts(1587): 37 in_visible_group
02-18 13:26:38.500: D/Contacts(1587): 38 starred
02-18 13:26:38.500: D/Contacts(1587): 39 link_count
02-18 13:26:38.500: D/Contacts(1587): 40 display_name_alt
02-18 13:26:38.500: D/Contacts(1587): 41 sort_key
02-18 13:26:38.500: D/Contacts(1587): 42 contact_presence
02-18 13:26:38.500: D/Contacts(1587): 43 contact_status_res_package
02-18 13:26:38.500: D/Contacts(1587): 44 contact_status_ts

1 个答案:

答案 0 :(得分:0)

以下代码显示了一种简单的方法来阅读所有电话号码和姓名:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext())
{
  String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
phones.close();