How can I programmatically join 2 contacts in andr

2020-07-06 07:08发布

问题:

I need to know if is it possible to join two or more contacts (in a programmatic way, using the Contacts android API or something).

For example, I have a contact "Axel Rose" with an email account and a phone number, and I've noticed that some apps like whatsapp, Facebook and Skype are creating new contact entries for Axel Rose, instead of merging the existing one.

I can join contacts using the "Join feature" from the phone, but is there a programmatic way?

Thanks in advance.
Cristian.

回答1:

You can use AggregationExceptions. See How to manual aggregate contacts ?

A good place to understand how contacts and aggregation works is Contacts Provider Documentation

Short summary:

The contacts that you see on your phonebook are the ones on the ContactsContract.Contacts table. The photo, phone, etc shown on the phonebook comes from different RawContacts entries. A contact can have one o several RawContacts.

A Contact happens to have several RawContact when the RawContacts share some data on common (name, phone number, email, etc). The union of this RawContacts into a single Contact is made by automatic aggregation rules (see Contact Basics docs).

You can't insert contacts on the ContactsContract.Contacts table. From the documentation:

Note: If you try to add a contact to the Contacts Provider with an insert(), you'll get an UnsupportedOperationException exception. If you try to update a column that's listed as "read-only," the update is ignored.