Insert RawContact with a specific contactid

2019-05-15 09:47发布

I am trying to create a rawcontact in android that has a specific contact id, so it is linked to other rawcontacts with the same contactid (not rawcontactid).

The problem is I am unable to insert the Contact_ID into the ContentProviderOpertations. Using the following code return "Insert failed"

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int id = (int) contactId;

    String condition = Data.RAW_CONTACT_ID + "=?";
    String[] parameters = { "" + id };

    try {

        String accountName = account.name;
        String accountType = account.type;
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, accountType)
                .withValue(RawContacts.ACCOUNT_NAME, accountName).build());
        ops.add(getAccountGroupOperation(account));

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.CONTACT_ID, "" + id)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, contact.getName())
                .build()); 
        MyApplication.getAppContext().getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        ...
    }

What am I doing wrong?

2条回答
爷的心禁止访问
2楼-- · 2019-05-15 10:13

You cannot do this. The Android contact aggregator aggregates raw contacts into contacts automatically. You should not need to worry about assigning a raw contact to a contact.

查看更多
神经病院院长
3楼-- · 2019-05-15 10:29

It is possible to do this but not in the way you want. You can make Contacts application join two raw contacts by adding both raw contact ids to AggregationExceptions table by setting the type as TYPE_KEEP_TOGETHER

http://developer.android.com/reference/android/provider/ContactsContract.AggregationExceptions.html

查看更多
登录 后发表回答