4

Can someone please explain me how to open contact native for specific contact URi

I manage to resolve the contact URI by:

Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(displayName));

and then I tried do this:

     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
     intent.putExtra(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, "555");
     context.startActivity(intent);

but it's only open the native address book without any resolving

and_dev
  • 1,483
  • 3
  • 14
  • 17

3 Answers3

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

You can retrieve the contactId by browsing using contentResolver:

id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(tag, "Id: "+ id+"\t Name: "+name);
StarsSky
  • 6,721
  • 6
  • 38
  • 63
1

After you get the cursor of contacts and the index of a particular contact, get the Uri and start activity with intent of ACTION_VIEW as below:

cursor.moveToPosition(position); // or cursor.moveToFirst() if single contact was selected.
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(ContactsContract.Contacts.getLookupUri(id, lookupKey));
try {
    context.startActivity(intent);
} catch (Exception e) {
    e.printStackTrace();
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

The accepted answer does not work when Android Messages is not the user's default SMS app. For some reason the intent goes through Android Messages before opening the contact details, and when the app is not default it takes you to their SMS permissions activity.

The below intent uses a slightly different URI: CONTENT_LOOKUP_URI instead of CONTENT_URI, and it works no matter which texting app the user is using.

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, String.valueOf(contactID));
intent.setData(uri);
context.startActivity(intent);
S01ds
  • 301
  • 5
  • 8