How to open a contact card in Android by ID

后端 未结 3 1285
一向
一向 2020-12-09 11:23

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


        
3条回答
  •  情深已故
    2020-12-09 11:33

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

提交回复
热议问题