gpt4 book ai didi

android - 获取 Android 手机联系人的跨设备唯一 ID

转载 作者:行者123 更新时间:2023-12-02 12:10:45 25 4
gpt4 key购买 nike

我需要从 Android 设备查询我正在处理的项目的联系人,并且我需要以一种可以将应用程序中的实例链接到电话簿中的联系人的方式来保存它们。
我发现 CONTACT_ID (这是对 _ID 的引用)每个联系人的可能会在设备之间发生变化,因此如果我切换到其他 Android 设备,该 ID 将无效。
临时解决方案是使用联系人的 SOURCE_ID ,这是一个字符串,用于将该行唯一标识到其源帐户。解决方案非常好,因为如果联系人来自(例如)Google 帐户,它将在我拥有的每台设备上保持完全相同的 ID。问题是 - 并非每个联系人都有 SOURCE_ID .
也可以使用它的数据作为过滤器来查询特定的联系人,它可以作为一个唯一的 ID,例如他的电话号码等……但是每条数据都有缺陷。例如:一个联系人可能有多个电话号码(这仍然可以)并且号码可以变化(例如:202-555-0105 与 +1-202-555-0105 相同,也与 ( 202) 555 0105 和 2025550105)。编辑:也不是每个联系人都有电话号码,那又如何?
所以在给出问题之后 -
如何为 Android 电话簿中的联系人获取唯一 ID,以便它们成为同一个跨设备?
注意:默认情况下在 IOS 上是可能的(见 documentation ) -

Contacts in different accounts that represent the same person may be automatically linked together. Linked contacts are displayed in OS X and iOS apps as unified contacts. A unified contact is an in-memory, temporary view of the set of linked contacts that are merged into one contact.

By default the Contacts framework returns unified contacts. Each fetched unified contact (CNContact) object has its own unique identifier that is different from any individual contact’s identifier in the set of linked contacts. A refetch of a unified contact should be done with its identifier.

最佳答案

你要找的是 LOOKUP_KEY

An opaque value that contains hints on how to find the contact if its row id changed as a result of a sync or aggregation.



获取联系方式 LOOKUP_KEY遍历您的联系人,这是一个示例:

注: <uses-permission android:name="android.permission.READ_CONTACTS" />是必须的。
val contentUri = ContactsContract.Contacts.CONTENT_URI
val cursor = context?.contentResolver?.query(contentUri, null, null, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY))
val lookupKey =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))
}
}
cursor?.close()

以下是使用 LOOKUP_KEY 检索联系人的方法。 :
val lookupKey = "0r1-3C263544104632"
val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
val uri = ContactsContract.Contacts.lookupContact(contentResolver, lookupUri)
val cursor = context?.contentResolver?.query(uri, null, null, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY))
}
}
cursor?.close()
}

请注意,联系人必须同步才能使用 LOOKUP_KEY。不能重新生成。
LOOKUP_KEY保证为每个联系人生成,但是,如果帐户未设置且未同步,则 LOOKUP_KEY每当联系人被修改时都会重新生成。

考虑到这一点,您将始终拥有一个独特的 LOOKUP_KEY如果设备已同步。 LOOKUP_KEY依赖谷歌云,这可能是苹果使用的相同解决方案。

Android 设备不太可能没有 Google 帐户,因为大多数 Android 用户都依赖 Google 服务。

恐怕这是拥有唯一标识符的最佳方法,但是,如果您愿意,可以将用户电话号码与其他联系方式结合起来,但不能保证这种方法有效,因为联系人可能会发生变化。如果您的用户已注册并且您将获得他们的信息,那么您可以在后端检查哪些哈希值符合您的期望,然后根据您自己的同步工作。

如果你想玩,我创建了一个 sample app通过实现,您可以查看联系人并找到查找键以及使用查找键检索联系人。

我还建议你看看 SyncAdapter .

关于android - 获取 Android 手机联系人的跨设备唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58118556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com