简体   繁体   中英

How to aggregate contact data, when trying to add new contact?

I'm building an app where you can download the address of any point of interest to your personal contacts.

I'm using this code, to add the contact. Found it here at Stackoverflow. ;)

ops = new ArrayList<ContentProviderOperation>();
            ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
   .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
   .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

//------------------------------------------------------ Names
if (title != null) {
   ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, title).build());
}

//------------------------------------------------------ Work Numbers
if (phone != null) {
   ops.add(ContentProviderOperation
       .newInsert(ContactsContract.Data.CONTENT_URI)
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN).build());
}

//------------------------------------------------------ Address
ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zip)
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street).build());

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

This works fine, as long as this contact not exists. Else, adding an entry will add other duplicate entries, even if all the data are exactly the same. When reading this article , especially the part about aggregation, I thought Android could handle that automatically. But obviously, it doesn't for me.

Is there an easy way to force Android to aggregate the new entry with existing entries, if they correspond?

This question is quite old but i solved this recently just incase someone else is stuck,try adding the aggregation mode as below. Add it where you creating your account

ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE)
                    .withValue(ContactsContract.RawContacts.AGGREGATION_MODE,
                            ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
                    .build());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM