如何在 ContactsContract.Contacts table 上唯一标识联系人

How to uniquely identify a contact on ContactsContract.Contacts table

我有一个应用程序可以获取设备上联系人的 ContactsContract.Contacts.LOOKUP_KEY 并将其保存在应用程序数据库中。

阅读 this 页面后,我认为我可以使用 LOOKUP_KEY 来唯一标识联系人,即使在编辑联系人时(例如编辑联系人姓名后)。

实际上我看到在编辑联系人后,它的 LOOKUP_KEY 发生了变化,所以我不能再使用我保存在我的应用程序数据库中的 LOOKUP_KEY

我的问题是:有没有一种方法可以唯一标识 ContactsContract.Contacts 上的联系人,从它在设备上首次创建到从设备中删除?

谢谢

每个名为 _ID 的联系人的行 ID(主键)。

LOOKUP_KEY 不能单独用作密钥,而是应该与联系人的 _ID 一起使用以形成完整的 lookupUri

然后可以使用 lookupUriCONTENT_LOOKUP_URI 表中查找联系人。 CONTENT_LOOKUP_URI基本上是先找_ID的联系人,如果找不到,或者_ID好像是错误的联系人,就用提示LOOKUP_KEY 部分尝试为您找到正确的联系人。

来自CONTENT_LOOKUP_URI

A content:// style URI for this table that should be used to create shortcuts or otherwise create long-term links to contacts. This URI should always be followed by a "/" and the contact's LOOKUP_KEY. It can optionally also have a "/" and last known contact ID appended after that. This "complete" format is an important optimization and is highly recommended.

As long as the contact's row ID remains the same, this URI is equivalent to CONTENT_URI. If the contact's row ID changes as a result of a sync or aggregation, this URI will look up the contact using indirect information (sync IDs or constituent raw contacts).

Lookup key should be appended unencoded - it is stored in the encoded form, ready for use in a URI.

来自getLookupUri(long contactId, String lookupKey)

Build a CONTENT_LOOKUP_URI lookup Uri using the given _ID and LOOKUP_KEY.

来自 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.