添加 RawContact 使其聚合到现有联系人
Posted
技术标签:
【中文标题】添加 RawContact 使其聚合到现有联系人【英文标题】:Add RawContact so it aggregates to an existing contact 【发布时间】:2011-09-25 08:05:25 【问题描述】:我正在尝试将新的 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();
【问题讨论】:
【参考方案1】:我想通了。我尝试将 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();
【讨论】:
这行得通,但我认为应该有更好的方法!根据谷歌文档,具有相同 Raw.Contact_id 的原始联系人项目聚合为一个联系人,我试过这个,但它不起作用:| @Sirlate 根据the RawContacts documentation,该字段是只读的。 添加显示名称的位置以上是关于添加 RawContact 使其聚合到现有联系人的主要内容,如果未能解决你的问题,请参考以下文章
如何使用联系人 id 和查找键获取 rawcontact id