简体   繁体   中英

Open contact intent DISPLAY_NAME

    String exactname = "Joe Blogs"

I've searched high and low of a way to directly open the contact using DISPLAY_NAME not the _ID of the contact.

EDIT: Joe Blogs is definitely a unique entry.

From this answer

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

Do I have to query the ID of Joe Blogs before I can open his contact with an intent similar to the above? Or have I (hopefully) missed something?

Thanks in advance.

Yes, you need to query your database with something like,

Cursor cur = getContentResolver().query(CONTENT_URI, null, DISPLAY_NAME + "=?", new String[] { "Joe Blogs" }, null);

Then retrieve the row returned by the Cursor with,

if (!cur.moveToFirst()) {
    // Then the cursor isn't empty. Get the contact's id.
    contactId = cur.getInt(cur.getColumnIndex(CONTACT_ID));
}

This will return the contact id associated with the first record in the Cursor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM