Skip to content Skip to sidebar Skip to footer

How To Open A Contact Card In Android By Id

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.I

Solution 1:

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).

Intentintent=newIntent(Intent.ACTION_VIEW);
    Uriuri= Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
    intent.setData(uri);
context.startActivity(intent);

Solution 2:

You would use the following URI:

Uri.BuildernewUriBuilder= 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.

Solution 3:

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:

Intentintent=newIntent(Intent.ACTION_VIEW);
StringlookupKey= phonesCursor.getString(phonesCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.LOOKUP_KEY));
longcontactId= phonesCursor.getLong(phonesCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
Uriuri= ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
intent.setData(uri);
startActivity(intent);

Post a Comment for "How To Open A Contact Card In Android By Id"