Android 联系方式如何更新?

发布于 2024-10-30 17:43:03 字数 1212 浏览 1 评论 0原文

我最近为我的应用程序创建了一个同步适配器,它将通过网络请求获取的联系人与手机中的联系人同步。添加联系人没有问题,但是当联系信息更改时,我无法正确更新联系信息。例如联系人的公司名称字段。以下是我尝试过的一些示例查询,但它们不起作用或仅部分起作用(即 - 一些联系人已更新但不正确):

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2024-11-06 17:43:03

在花费了大量时间试图找出正确的查询之后,我相信我已经找到了答案。看来我需要将 BaseColumns._ID 更改为 ContactsContract.Data.CONTACT_ID 并且对于我所做的每次更新,我还必须提供 mime 类型在 android 文档中的任何地方都没有看到这一点。这篇文章提供了很多帮助:使用 Android 联系人

        String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] orgWhereParams = new String[]{String.valueOf(id), 
        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
    operationList
    .add(ContentProviderOperation
            .newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(orgWhere, orgWhereParams)
            .withValue(
                    ContactsContract.CommonDataKinds.Organization.DATA,
                    guCon.getCompany()).build());

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 to ContactsContract.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

        String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] orgWhereParams = new String[]{String.valueOf(id), 
        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
    operationList
    .add(ContentProviderOperation
            .newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(orgWhere, orgWhereParams)
            .withValue(
                    ContactsContract.CommonDataKinds.Organization.DATA,
                    guCon.getCompany()).build());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文