Android contacts: how does the find key work?
In addition to the contact ID, Android also got look_ Up key. Since the ID of the contact can be changed, you can use look_ Up key to get user URI
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);
}
But how does it work? The source code of contacts.lookupcontact does not introduce much about the actual implementation. Then, who can explain how they try to do this?
/**
* 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;
}
Another thing I tested was to merge two contacts using contactscontract. Aggregationexceptions, and then mine. Two looks for the contact URI_ The generation of up keys has the same contact URI as expected
So what do they do?
resolvent:
Since the contact ID may change from time to time (for example, when the contact synchronization is broken and the contact needs to be resynchronized from the server), Android introduces the concepts of lookupkeys and lookupuris
Lookupkey is an opaque value that can be internally converted into a group of fields by the contacts framework: Contact ID, raw contact ID, primary display names, etc
Whenever you try to access a contact through lookupuri, the system will extract the lookupkey from the URI, try to access the contact ID, and compare other fields (original ID, name, etc.) with the found contact (if any). If it seems to be the correct contact, it will return it. If the contact ID is not found, or the system detects that it is the wrong contact, All contacts are queried to find the correct contact (using the auxiliary field stored on the key)
Therefore, lookupkey serves as a quick way to return the contact ID or search in case of adverse conditions