16

Is it possible to open an android contact card by contact's ID? It works with the phone-number. Here is an example, if I use

Intent i = new Intent();
i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
i.setData(Uri.fromParts("tel", "123456", null)); //<---- Change here from Phone to IDcontext.startActivity(i);

But I want to open this contact card by ID, for example if the phone-number from the contact would change.

3 Answers 3

44

use ACTION_VIEW and either build a contact URI using the contact ID or use the contact lookup URI if you already have it (preferred).

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
    intent.setData(uri);
context.startActivity(intent);
4
  • 4
    Just as a side note: It's discouraged to pick contacts by id, because it's not guaranteed that it won't change. In fact the chances are very high that the id WILL change. You should use jhominal's solution and pick it up with LOOKUP_URI, because lookup key always stays same. Background: The ID can be changed during synchronizations, so don't depend on it or your app may break at any time in future during or after a contact list synchronization
    – Tseng
    Nov 25, 2010 at 10:34
  • Wow, so obviously missed the meaning of "id"... thank you Tseng, I'll change it in my code and remember for the next time :)
    – Java_Waldi
    Nov 25, 2010 at 11:01
  • Yea, had to go through that thing when I ported one of my Apps from 1.6 to 2.x as the whole Contact thing was changed and old stuff broke
    – Tseng
    Nov 25, 2010 at 11:28
  • Hy i need to call Intent.ACTION_PICK because i need to select number and fetch from my application but its not working..any idea than please suggest me how its possible ?
    – SBJ
    Jul 29, 2011 at 15:35
6

You would use the following URI:

Uri.Builder newUriBuilder = ContactsContract.Contacts.CONTENT_LOOKUP_URI.buildUpon();
newUriBuilder.appendPath("/").appendPath(theContactKey)
i.setData(newUriBuilder.build());

You will find more details about how this URI works by looking at the API documentation for CONTENT_LOOKUP_URI.

2
  • 6
    Ok, thank you jhominal for pointing in the right direction, but your answer was not completely right. Here is the right solution: Uri look = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, hereTheLookupKey); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(look);
    – Java_Waldi
    Nov 30, 2010 at 15:13
  • isn't using this URI better: Contacts.getLookupUri(contactId, lookupKey) ? Dec 31, 2016 at 18:51
4

I was trying to open a contact card using the listed here methods, but somehow the contacts activity was closing immediately after it was opening.

it seemed that the contact activity didn't accept my old content uri.

I resolved this problem using the getLookupUri (long contactId, String lookupKey) method of ContactsContract.Contacts class for obtaining the right content uri https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#getLookupUri(long, java.lang.String)

So the code for opening a contact card becomes:

Intent intent = new Intent(Intent.ACTION_VIEW);
String lookupKey = phonesCursor.getString(phonesCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.LOOKUP_KEY));
long contactId = phonesCursor.getLong(phonesCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
intent.setData(uri);
startActivity(intent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.