安卓android的联系人的contacts, raw contacts, and data的区别

https://stackoverflow.com/questions/5151885/android-new-data-record-is-added-to-the-wrong-contact/5158059#5158059

https://developer.android.com/guide/topics/providers/contacts-provider.html

https://stackoverflow.com/questions/35448250/how-to-get-whatsapp-contacts-from-android/35453979#35453979

https://developer.android.com/reference/android/provider/ContactsContract.RawContactsEntity.html

https://developer.android.com/reference/android/provider/ContactsContract.Data.html

 

 

 

Couple of Key pointers:

  • Contacts._ID = Data.CONTACT_ID
  • RawContacts._ID = Data.RAW_CONTACT_ID
  • RawContacts.CONTACT_ID = Contacts._ID
  • RawContactsEntity._ID = RawContacts._ID

Sounds confusing?? Let me try...

  1. The Contacts database is divided into 3 tables contactsraw contacts, and data.
  2. Each table contains column (_ID) which is an auto incremented primary key.
  3. data table contains all the contact info like phone number, mail id, address etc.
  4. The raw contacts points to the actual contact created. Hence we use the raw contacts while adding a contact.
  5. The user cannot add any data in the contacts table. The data in this table is populated internally due to aggregation of contacts.
  6. The reason your logic worked for some of the contacts is: _ID for contactsraw contactsremains same until there is any contact aggregation taking place. Lets say you add two contacts with same name abc. Here the _ID for raw contacts increments twice while _ID forcontacts increments only once as these two contacts gets merged due to the aggregation of contacts

Refer this for more details.

The best approach to fetch the info in your case is by using ContactsContract.RawContactsEntity ( an outer join of the raw_contacts table with the data table)

Reference:http://developer.android.com/reference/android/provider/ContactsContract.RawContactsEntity.html

 

 

here is the show Android Contact Data Store Diagram

posted @ 2017-07-14 09:57  微信公众号--共鸣圈  阅读(1361)  评论(0编辑  收藏  举报