Add RawContact so it aggregates to an existing contact

耗尽温柔 提交于 2019-11-30 12:04:07

I figured it out. I tried replacing the CommonDataKinds.StructuredName row with a CommonDataKinds.Phone row that contains the same number as my original contact and then it aggregates the RawContacts correctly.

The working code looks like this

public static void addContactTag(Context context, Account account, String number) {

    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

    //  Create our RawContact                                                                                                           
    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
    builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
    operationList.add(builder.build());

    //  Create a Data record of common type 'Phone' for our RawContact                                                                  
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
    operationList.add(builder.build());

    //Create a Data record of custom type "vnd.android.cursor.item/vnd.be.ourservice.profile" to display a link to our     profile    
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, OURSERVICE_MIMETYPE);
    builder.withValue(ContactsContract.Data.DATA1, number);
    builder.withValue(ContactsContract.Data.DATA2, "ourservice user");
    builder.withValue(ContactsContract.Data.DATA3, "Go to ourservice profile");
    operationList.add(builder.build());


    try {
        mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
        Log.d(Global.TAG, "addContact batch applied");
    } catch (Exception e) {
        Log.e(Global.TAG, "Something went wrong during creation! " + e);
        e.printStackTrace();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!