Android联系人:查找键如何工作? [英] Android contacts: How does the lookup key works?

查看:74
本文介绍了Android联系人:查找键如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了联系人 id 之外,Android还获得了 LOOK_UP 键.由于联系人的 id 可以更改,因此您可以使用 LOOK_UP 键获取用户uri.

On top of contacts id, Android also got LOOK_UP key. Since id of contact can change, you can obtain user uri, using LOOK_UP key.

public static Uri lookupContactUri(String lookup, Context context){
    ContentResolver contentResolver = context.getContentResolver();
    Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup);
    return ContactsContract.Contacts.lookupContact(contentResolver, lookupUri);
}

但是它如何工作? Contacts.lookupContact的源代码并没有告诉您有关实际实现的更多信息.那么谁能解释一下他们如何设法做到这一点?

But how does it work? The source code of the Contacts.lookupContact doesn't tell much about the actual implementation. So can anyone explain how does they manage to pull this up?

    /**
     * Computes a content URI (see {@link #CONTENT_URI}) given a lookup URI.
     * <p>
     * Returns null if the contact cannot be found.
     */
    public static Uri lookupContact(ContentResolver resolver, Uri lookupUri) {
        if (lookupUri == null) {
            return null;
        }

        Cursor c = resolver.query(lookupUri, new String[]{Contacts._ID}, null, null, null);
        if (c == null) {
            return null;
        }

        try {
            if (c.moveToFirst()) {
                long contactId = c.getLong(0);
                return ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
            }
        } finally {
            c.close();
        }
        return null;
    }

我测试的另一件事是使用 ContactsContract.AggregationExceptions ,然后为联系uri进行采石.两个 LOOK_UP 键的产生都与预期的联系人uri相同.

Another thing I tested, is merging two contacts using ContactsContract.AggregationExceptions and then quarrying for contact uri. Both of the LOOK_UP keys yield with the same contact uri as expected.

那他们怎么办呢?

推荐答案

由于联系人ID可能会不时更改(例如,当联系人同步被破坏并且需要从服务器重新同步联系人时),Android引入了LookupKeys和LookupUris.

Since contact ids can change from time to time (e.g. when contacts sync is corrupted and contacts needs to be resynced from server), Android introduced the concept of LookupKeys and LookupUris.

LookupKey是一个opaque值,可以在Contacts框架内部将其转换为一组字段:contact-idraw-contact-idsprimary-display-names等.

A LookupKey is an opaque value, that internally can be translated by the Contacts framework to a set of fields: contact-id, raw-contact-ids, primary-display-names, etc.

每当您尝试通过LookupUri访问联系人时,系统都会从Uri中提取LookupKey,尝试访问contact-id,并比较其他字段(原始ID,名称等). )到找到的联系人,如果看似正确的联系人,则将其返回. 如果未找到contact-id,或者系统检测到它是错误的联系人,则会对所有联系人进行查询以找到正确的联系人(使用存储在该键上的辅助字段).

Whenever you try to access a contact via a LookupUri, the system, extracts the LookupKey from the Uri, tries to access the contact-id, and compares the other fields (raw-ids, names, etc.) to the found contact, if it seems the right contact, it returns it. If the contact-id wasn't found, or the system detects it's the wrong contact, a query is made over all contacts to find the right one (using the auxiliary fields stored on that key).

因此LookupKey用作返回contact-id的快速方法,或者在发生不良情况时进行搜索.

So the LookupKey acts as a quick method to either return the contact-id, or search for it in case something bad happened.

这篇关于Android联系人:查找键如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆