簡體   English   中英

Android,無法訪問聯系人電話號碼

[英]Android, cant access contacts phone numbers

我無法訪問聯系人的電話號碼,也無法找出原因。

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

以下代碼顯示了一種讀取所有電話號碼和姓名的簡便方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM