11

I'm currently writing a application that allows to save drafts (using android version >= 2.0). Each draft is connected to a contact via the ContactsContract.Contacts.LOOKUP_KEY. My problem is that if I change the name of my contact the lookup key changes also. Is that the way this works?

So for what do I need a lookup key? I thought that the lookup key does never change and now it changes anyway. I'm confused about that behavior ...

Can someone explain to me how to link permanently to a contact? Should I use IDs instead of the lookup key?

Thanks in advance.

Gaket
  • 6,533
  • 2
  • 37
  • 67
dotcs
  • 2,286
  • 1
  • 18
  • 26

2 Answers2

17

It is my understanding that the lookup key is a structured / hierarchical key. Hence strictly speaking it can change, but still be used to find your contact back, by using the appropriate method:

    Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
    Uri res = ContactsContract.Contacts.lookupContact(getContentResolver(), lookupUri);
Flow
  • 23,572
  • 15
  • 99
  • 156
BoD
  • 10,838
  • 6
  • 63
  • 59
  • Yeah, that seems to work. Somehow I've not taken notice of the 'ContactsContract.Contacts.lookupContact()' method. Thanks for your answer. – dotcs Oct 01 '10 at 11:16
  • 1
    I am working with Android 2.2. I accessed a lookup key for contact it was 0r12-2982324852 later I changed the name and saved the contact and lookupkey was 0r12-2982324852365430403C4638. So it is same for Android 2.2 also, but when I added a phone number to the same contact lookup key remained the same 0r12-2982324852365430403C4638. So can we conclude what @BoD said is correct. – Gaurav Agarwal Jun 12 '12 at 15:56
  • I don'ty understand how the link can be "permanent". If I store a lookup key in my application table, How can I retrieve the contact with this key if the name and contact ID change ? the lookup key will be different and the contact URI will change as well so how can I save in my DB a permanet reference to this contact ?? – ThierryC Nov 22 '17 at 14:53
4

Edited:

Why don't you find contact id or lookup key via using raw contact id? this is bug in 2.1.

Lookup key was based on the contact name for unsynced contacts.

http://comments.gmane.org/gmane.comp.handhelds.android.devel/130677

==================================================================

I didn't try yet. But I found some information about this.

http://developer.android.com/resources/articles/contacts.html

....

If performance is a concern for your application, you might want to store both the lookup and the long ID of a contact and construct a lookup URI out of both IDs, as shown here:

Uri lookupUri = getLookupUri(contactId, lookupKey)

When both IDs are present in the URI, the system will try to use the long ID first. That is a very quick query. If the contact is not found, or if the one that is found has the wrong lookup key, the content provider will parse the lookup key and track down the constituent raw contacts. If your app bulk-processes contacts, you should maintain both IDs. If your app works with a single contact per user action, you probably don't need to bother with storing the long ID.

erakitin
  • 11,437
  • 5
  • 44
  • 49
Brad Hong
  • 389
  • 2
  • 11