Adding RawContacts to Aggregate Existing Contacts
This article will guide you through the process of adding raw contacts to an existing contact, resulting in a more comprehensive and aggregated contact record.
Understanding RawContacts
In Android, contacts are built using a concept called “RawContacts.” Each RawContact represents a single source of contact information, such as a SIM card, Google account, or a phone’s internal memory.
When multiple RawContacts share the same identifier (usually the phone number), the system aggregates them into a single, unified Contact record.
Steps to Add RawContacts
- Get the Existing Contact ID:
- Create a New RawContact:
- Set the RawContact’s Data (e.g., Phone Number):
- Link the RawContact to the Existing Contact:
- Insert the RawContact into the ContentResolver:
String contactId = "YOUR_CONTACT_ID"; // Replace with the existing Contact ID
RawContact rawContact = new RawContact();
Data phoneData = new Data(); phoneData.setData1("YOUR_PHONE_NUMBER"); phoneData.mimetype = "vnd.android.cursor.item/phone_v2"; phoneData.setData2("YOUR_PHONE_NUMBER_TYPE"); rawContact.add(phoneData);
rawContact.setContactId(Long.parseLong(contactId));
ContentResolver cr = getContentResolver(); cr.insert(ContactsContract.RawContacts.CONTENT_URI, rawContact.toContentValues());
Example: Aggregating a New Phone Number
This code snippet demonstrates adding a new phone number to an existing contact, using the steps outlined above:
// Assuming you have an existing contact with the ID "12345" String contactId = "12345"; // Create a new RawContact RawContact rawContact = new RawContact(); // Set the RawContact's phone number data Data phoneData = new Data(); phoneData.setData1("1234567890"); // New phone number phoneData.mimetype = "vnd.android.cursor.item/phone_v2"; phoneData.setData2(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); rawContact.add(phoneData); // Link the RawContact to the existing contact rawContact.setContactId(Long.parseLong(contactId)); // Insert the RawContact ContentResolver cr = getContentResolver(); cr.insert(ContactsContract.RawContacts.CONTENT_URI, rawContact.toContentValues());
Comparison Table
Method | Description | Example |
---|---|---|
Create New Contact | Creates a brand-new contact with a separate ID. | ContactsContract.Contacts.CONTENT_URI |
Add RawContact | Links new information to an existing contact, aggregating data. | ContactsContract.RawContacts.CONTENT_URI |
Conclusion
By using the RawContacts API, you can seamlessly add new data to existing contacts, creating a unified and comprehensive view of your contact information.