添加RawContact,使其聚合到现有联系人

时间:2011-06-29 16:43:15

标签: android contacts aggregation rawcontacts

我正在尝试向现有联系人添加新的RawContact,以便我的自定义数据字段显示在原始联系人中。我尝试将StructuredName数据行添加到我的新RawContact,其DisplayName与原始RawContact的DisplayName相匹配。我认为匹配的DisplayNames足以聚合两个RawContacts,但联系人应用程序似乎将RawContacts显示为不同的联系人。

这是我的代码

  public static void addContact(Context context, Account account, String number, String displayname) {
    Log.e(Global.TAG, "adding contact: " + number + " / " + displayname);

    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 'StructuredName' for our RawContact
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayname);
    operationList.add(builder.build());

    //Create a Data record of custom type "vnd.android.cursor.item/vnd.be.ourservice.profile" to display a link to the     profile
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.be.ourservice.profile");
    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();
    }
   }

1 个答案:

答案 0 :(得分:15)

我明白了。我尝试用CommonDataKinds.Phone行替换CommonDataKinds.StructuredName行,该行包含与原始联系人相同的编号,然后正确聚合RawContacts。

工作代码如下所示

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();
    }
}