Android 联系方式如何更新?
我最近为我的应用程序创建了一个同步适配器,它将通过网络请求获取的联系人与手机中的联系人同步。添加联系人没有问题,但是当联系信息更改时,我无法正确更新联系信息。例如联系人的公司名称字段。以下是我尝试过的一些示例查询,但它们不起作用或仅部分起作用(即 - 一些联系人已更新但不正确):
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company");
context.getContentResolver().update(Uri.parse("content://com.android.contacts/data/"), values, BaseColumns._ID + "=?", new String[] { String.valueOf(id) } );
我也尝试按照 android 文档的建议批量执行此操作:
builder = ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(BaseColumns._ID + " =?", new String[]{String.valueOf(id)});
builder.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
"New Company Name!");
operationList.add(builder.build());
我已阅读 ContactContracts Documentation 最初遵循此 教程。我还检查了 api 中的 AuthenticatorActivity 示例,但无济于事。非常感谢任何帮助。
I recently created a Sync adapter for my app, It will sync contacts I am getting via a web request with the contacts in the phone. I have no problem adding the contact, however I cannot get the contact information to correctly update when contact information has changed. For example the Company Name field on the contact. Here is some example queries I have tried that did not work or only partially worked(ie - some contacts updated but not correctly):
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company");
context.getContentResolver().update(Uri.parse("content://com.android.contacts/data/"), values, BaseColumns._ID + "=?", new String[] { String.valueOf(id) } );
I have also tried doing this in batch as suggested by the android documentation:
builder = ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(BaseColumns._ID + " =?", new String[]{String.valueOf(id)});
builder.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
"New Company Name!");
operationList.add(builder.build());
I have read the ContactContracts Documentation and originally was following this tutorial. I also checked into the AuthenticatorActivity
example in the api's to no avail. Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在花费了大量时间试图找出正确的查询之后,我相信我已经找到了答案。看来我需要将
BaseColumns._ID
更改为ContactsContract.Data.CONTACT_ID
并且对于我所做的每次更新,我还必须提供 mime 类型在 android 文档中的任何地方都没有看到这一点。这篇文章提供了很多帮助:使用 Android 联系人After spending an exhausting amount of time trying to figure out the correct query, I believe I have found the answer. It looks like i needed to change the
BaseColumns._ID
toContactsContract.Data.CONTACT_ID
and for each update I made, I also had to supply the mime-type also I did not see this anywhere in the android documentation. Much help was found on this write-up: Working With Android Contacts