Fetching a contact with lookupkey gives unexpected contat-id

533 views Asked by At

So I have a problem..

Im receiving a list of contacts by doing:

mContentResolver.query(Contacts.CONTENT_URI, CONTACTS_PROJECTION, null, null, null);

and for each contact I extract a lookup key and a contact id by doing:

String lookupKey = cursor.getString(ContactsColumns.LOOKUP_KEY);
int contactId = cursor.getInt(ContactsColumns._ID);

Given that I can fetch entities from the content directory:

Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
    contactUri = Uri.withAppendedPath(contactUri, Entity.CONTENT_DIRECTORY);
    Cursor detailCursor = mContentResolver
            .query(contactUri, ENTITY_PROJECTION, SELECTION_ENTITY, SELECTION_ARGS_ENTITY, null);

So far so good. The problem starts when I try to fetch a single contact given only its lookup key. I get the contact cursor by doing:

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Uri res = ContactsContract.Contacts.lookupContact(mContentResolver, lookupUri);
Cursor cursor = mContentResolver.query(res, CONTACTS_PROJECTION, null, null, null);

The problem now is that I get a different contact id. The contact information belongs to the same actual person but is different. Probably there are more than one contact connected to the lookup key.

So when I now try to receive the entity data don't get the same result due to the different contact id which in turn generates a different content uri.

For example, In the first case I get:

content://com.android.contacts/contacts/lookup/<lookupkey>/1008/entities

And in the second case I get:

content://com.android.contacts/contacts/lookup/<lookupkey>/511/entities

How should I use the lookup key in order to get the same contact as from which I got the lookup key in the first place?

Thanks!

1

There are 1 answers

0
nesomis On

Look at this answer [https://stackoverflow.com/a/4312261][1].
Also, read this comment in the same post

Those APIs are not exclusively for SyncAdapters, and lookup URIs are valid for the lifetime of the contact. (But of course, you still have to code for the case that contacts you're tracking can be deleted while your app isn't running). – Reuben Scratton Nov 30, 2010 at 12:51

It helps me.