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

Posted

技术标签:

【中文标题】Android,无法访问联系人电话号码【英文标题】:Android, cant access contacts phone numbers 【发布时间】:2013-02-03 03:18:29 【问题描述】:

我无法访问我的联系人的电话号码,也不知道为什么。

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】:

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

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();

【讨论】:

以上是关于Android,无法访问联系人电话号码的主要内容,如果未能解决你的问题,请参考以下文章

无法使用网络访问受限的 Agora API 拨打电话(Android 平台)

访问联系人的联系电话

为啥使用 quickblox sdk 从网站到 Android 在挂断电话上无法访问更新的会话信息?

Android - 无法获取某些联系人的电话号码

访问联系人 iOS 目标 c

android手机,新建联系人在通讯录无法显示。