添加 RawContact,以便聚合到现有联系人
我正在尝试将新的 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();
}
}
I am trying to add a new RawContact to an existing Contact so my custom data field shows up inside the original Contact. I tried Adding a StructuredName Data row to my new RawContact with a DisplayName that matches the DisplayName of the original RawContact. I thought matching DisplayNames would be enough to aggregate both RawContacts but the contacts app seems to display both RawContacts as different Contacts.
Here is my code
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。我尝试用包含与原始联系人相同号码的 CommonDataKinds.Phone 行替换 CommonDataKinds.StructuredName 行,然后正确聚合 RawContacts。
工作代码如下所示
I figured it out. I tried replacing the CommonDataKinds.StructuredName row with a CommonDataKinds.Phone row that contains the same number as my original contact and then it aggregates the RawContacts correctly.
The working code looks like this