2012-09-21 59 views
8

我在Android 2.3.3 API Level 10上。我想讀取與它們相關聯的所有聯繫人和數據,例如,所有電子郵件地址,電話號碼,自定義字段... 我想它是這樣的:閱讀所有聯繫人數據

Uri contactUri = ContactsContract.Contacts.CONTENT_URI; 
Cursor contacts = managedQuery(contactUri, null, null, null, null); 

contacts.moveToFirst(); 

do { 
    for(int i=0;i<contacts.getColumnCount();i++) 
    { 
     System.out.println(contacts.getColumnName(i) + ": " + contacts.getString(i)); 
    } 

    System.out.println("============\n\n"); 

} while (contacts.moveToNext()); 

contacts.close(); 

但是這給了我只有以下字段:

times_contacted: 0 
contact_status: null 
custom_ringtone: null 
has_phone_number: 1 
phonetic_name: null 
phonetic_name_style: 0 
contact_status_label: null 
lookup: [removed] 
contact_status_icon: null 
last_time_contacted: 0 
display_name: [removed] 
sort_key_alt: [removed] 
in_visible_group: 1 
_id: 101 
starred: 0 
sort_key: [removed] 
display_name_alt: [removed] 
contact_presence: null 
display_name_source: 40 
contact_status_res_package: null 
contact_chat_capability: null 
contact_status_ts: null 
photo_id: null 
send_to_voicemail: 0 

哪裏有電話號碼,電子郵件地址...?感謝您的任何提示!

回答

8

查看聯繫合同API的例子

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
      null, null, null, null); 
    if (cur.getCount() > 0) { 
    while (cur.moveToNext()) { 
     String id = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts._ID)); 
    String name = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
     //Query phone here. Covered next 
     } 
     } 
} 
} 

看看這個例子

http://code.google.com/p/android-contacts-contract-example/

+0

謝謝鏈接到谷歌的代碼示例,並從鏈接的教程那裏(http://app-solut.com/blog/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/)真的很有用:-) –

+0

我怎樣才能讀取它的地址和其他字段 – Nepster