如何在 Kotlin 中编辑联系人的电话号码?

发布于 2025-01-13 12:58:46 字数 316 浏览 3 评论 0原文

我有一个具有唯一联系人 ID 的联系人数据类,并且想要更改联系人的电话号码。 这是数据类:

data class Contact(
        val id : String,
        val name : String,
        val number : String)

我找到了一个关于如何有目的地执行此操作的教程,但是我想使用 Android 中的 contactProvider 来执行此操作。我已经在清单中声明了 WRITE_CONTACT 权限,但仍然不知道如何将电话号码保存到现有联系人。我在谷歌上也找不到任何好的教程。

I have got a contact data class with the unique contact ID and want to change the phone number of the contact.
Here's the data class:

data class Contact(
        val id : String,
        val name : String,
        val number : String)

I have found a tutorial on how to do this with an intent, however I would like to do this using the contactsProvider in Android. I already have WRITE_CONTACT permissions declared in the manifest, but still don't know on how to save the phone number to an existing contact. I'm also unable to find any good tutrial on Google.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无所谓啦 2025-01-20 12:58:46

以下是一段代码,用于将给定联系人 ID 的当前电话号码更新为新电话号码:

private fun updatePhone(contactId:Long, existingNumber:String, newNumber:String) {
  val contentValues = ContentValues()
  contentValues.put(Phone.NUMBER, newNumber)

  val where = Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "=?" + " AND " + Phone.NUMBER + "=?"
  val whereArgs = arrayOf<String>((contactId).toString(), Phone.CONTENT_ITEM_TYPE, existingNumber)

  contentResolver.update(Data.CONTENT_URI, contentValues, where, whereArgs)
}

以下是此代码背后的更多说明:https://stackoverflow.com/a/63761333/819355

我会将此问题标记为链接问题的重复项,但链接问题不会有任何接受的答案,所以它不会让我。

Here's a snippet of code that will update a current phone number to a new phone number for a given contact ID:

private fun updatePhone(contactId:Long, existingNumber:String, newNumber:String) {
  val contentValues = ContentValues()
  contentValues.put(Phone.NUMBER, newNumber)

  val where = Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "=?" + " AND " + Phone.NUMBER + "=?"
  val whereArgs = arrayOf<String>((contactId).toString(), Phone.CONTENT_ITEM_TYPE, existingNumber)

  contentResolver.update(Data.CONTENT_URI, contentValues, where, whereArgs)
}

Here's some more explanation behind this code: https://stackoverflow.com/a/63761333/819355

I would have marked this question as a duplicate of the linked question, but the linked question doesn't have any accepted answer so it won't let me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文