in your addContact
code you're missing the part that tells Contacts DB
to join your new raw-contact into the existing contact, so that contact will now contain your raw-contact and your app-specific line will be shown when opening that contact in the contacts app.
Check this answer on how to join a RawContact
into an existing Contact: why won't contacts aggregate?
You'll probably need to pass in some RawContact ID
to your addContact method so it'll be able to join the two together.
UPDATE
Instead of applying the aggregation operation together with your RawContact
insert operation, let's try to separate into two applyBatch
calls, also, let's aggregate your new raw-contact with ALL existing raw-contacts, not just one of them.
Try the following code, make sure you pass to it the existing contact-id (not raw-contact id) and your new raw-contact-id.
private void joinIntoExistingContact(long existingContactId, long newRawContactId) {
// get all existing raw-contact-ids that belong to the contact-id
List<Long> existingRawIds = new ArrayList<>();
Cursor cur = getContentResolver().query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=" + existingContactId, null, null);
while (cur.moveToNext()) {
existingRawIds.add(cur.getLong(0));
}
cur.close();
Log.i("Join", "Found " + existingRawIds.size() + " raw-contact-ids");
List<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// go over all existing raw ids, and join with our new one
for (Long existingRawId : existingRawIds) {
Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI);
builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, newRawContactId);
builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, existingRawId);
ops.add(builder.build());
}
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
P.S.
don't open two duplicate questions, one is enough.
ANOTHER UPDATE
You seem to have some confusion about IDs.
There are Data
IDs, RawContact
IDs, and Contact
IDs.
CommonDataKinds.Phone._ID
will return a Data
ID, identifying the specific row in the Data table in which that phone number is stored.
You can get a from the Phone
table the other IDs as well, use:
CommonDataKinds.Phone.RAW_CONTACT_ID
CommonDataKinds.Phone.CONTACT_ID
You can read more here:
https://stackoverflow.com/a/50084029/819355