Search code examples
contactsandroid-contactscontactscontract

How to launch contact detail activity of directory contact in android


I am Trying to launch the detail page of directory contacts(Some organigation contacts) with contact id. for local contacts it is working fine but not working for organigation contacts.

Here is my code. (name is contact name ,idstr is directory id )

lookupByName = ContactsContract.Contacts.CONTENT_FILTER_URI.buildUpon().appendEncodedPath(name)
                        .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, idStr).build();


            mCursor = mContext.getContentResolver().query(lookupByName, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);


            if (mCursor.moveToFirst()) {
                idPhone = 
                Long.valueOf(mCursor.getString(
            mCursor.getColumnIndex(ContactsContract.PhoneLookup._ID)));        
           }
   Intent intent = new Intent(Intent.ACTION_VIEW);                    
   intent.setData(ContentUris.
       withAppendedId(ContactsContract.Contacts.CONTENT_URI, idPhone ));        
  startActivity(intent);

Please help me.

Thanks in advance.


Solution

  • This is tricky, but managed to get it working You need to get the contact's LOOKUP_KEY, build a LookupUri from it, append the DIRECTORY_PARAM_KEY to the LookupUri, and put that in the intent's setData.

    String name = "hello";
    String directoryId = "5"
    
    Uri uri = Contacts.CONTENT_FILTER_URI.buildUpon().appendPath(name).appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, directoryId).build();
    String[] projection = new String[]{Contacts._ID, Contacts.DISPLAY_NAME, Contacts.LOOKUP_KEY};
    Cursor cur = getContentResolver().query(uri, projection, null, null, null);
    DatabaseUtils.dumpCursor(cur); // debug
    
    // add some safety checks first obviously...
    cur.moveToFirst();
    String lookup = cur.getString(2);
    Uri lookupUri = Contacts.getLookupUri(cur.getLong(0), lookup).buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, directoryId).build();
    
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(lookupUri);
    startActivity(intent);