将新连接添加到现有的Android联系人
gyus.
我正在尝试将连接添加到现有联系人,就像viber一样:
I'm trying to add a connection to an existing contact, like viber does:
在nemezis repo 之后添加具有这种连接的联系人非常简单,但是我没有设法更新联系人以添加连接.我已经尝试过:
It was pretty simple to add a contact with such connection following nemezis repo, but I haven't managed to update a contact in order to add a connection. I have tried:
ops.add(ContentProviderOperation.newUpdate(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
.withSelection(Data.RAW_CONTACT_ID + "= ?", new String[]{String.valueOf(id)})
//.withValue(Data.RAW_CONTACT_ID, id)
.withValue(Data.MIMETYPE, MIMETYPE_PROFILE)
//.withValue(Data.DATA1, 12345)
.withValue(Data.DATA2, "sample")
.withValue(Data.DATA3, "sample")
.build());
但是它不会没有错误就无法正常工作.我尝试了许多其他选项,但也都失败了,我怀疑是否应该将所有选项都张贴在这里.大家有什么想法吗?
But it just doesn't work without errors. I tried a bunch of other options, but failed with them too, I doubt I should post all of them here. Any ideas, guys?
非常感谢.
没关系,伙计们.我已经知道了.
Nevermind, guys. I have figured it out.
android联系人系统使用匹配.而且ACCOUNT_TYPE信息只是一次读/写数据.因此,您需要做的是创建一个新的原始联系人,并将其数据与现有联系人匹配(使用显示名称,电话号码或电子邮件).插入数据后,android会调用匹配并汇总帐户的数据.代码片段:
The android contact system uses matching. Moreover ACCOUNT_TYPE info is just read/write-once data. Therefore what you need to do is to create a new raw contact with the data matching with existing contact(using display name, phone number or email). Once you insert data android calls matching and aggregate the accounts. Piece of code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int backId = 0;
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true))
.withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
.withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
.build());
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
.withValueBackReference(Data.RAW_CONTACT_ID, backId)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
.withValueBackReference(Data.RAW_CONTACT_ID, backId)
.withValue(Data.MIMETYPE, MIMETYPE)
//.withValue(Data.DATA1, 12345)
.withValue(Data.DATA2, "data2")
.withValue(Data.DATA3, "data3")
.build());
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}