11

我正在尝试将新的 RawContact 添加到现有联系人中,以便我的自定义数据字段显示在原始联系人中。我尝试使用与原始 RawContact 的 DisplayName 匹配的 DisplayName 将 StructuredName Data 行添加到我的新 RawContact。我认为匹配的 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();
    }
   }
4

1 回答 1

15

我想到了。我尝试将 CommonDataKinds.StructuredName 行替换为 CommonDataKinds.Phone 行,该行包含与我的原始联系人相同的号码,然后正确聚合 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();
    }
}
于 2011-07-15T07:11:32.750 回答