Insert RawContact with a specific contactid

青春壹個敷衍的年華 提交于 2019-12-22 09:48:38

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/8741788/insert-rawcontact-with-a-specific-contactid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!