Pick contact directly from contact picker intent

Hardik Trivedi picture Hardik Trivedi · Dec 26, 2016 · Viewed 15.8k times · Source

Hello I want to pick a contact from our default contact book intent. I tried several ways to do it. Please find the code below. The problem with all those code is that they open one intermediate documents screen with few options there user has to select contact and than it opens contact book.

private void openContactIntent() {
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT, ContactsContract.Contacts.CONTENT_URI);
     intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
     startActivityForResult(intent, REQ_CONTACT_DIRECTORY);
}

I also tried

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

and

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT); 

What I see as an intermediate screen is enter image description here

Answer

schnill picture schnill · Jan 19, 2017

I also had the same problem. Finally, I got rid of intermediate picker screen using below code,

Intent i=new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, SELECT_PHONE_NUMBER);

In onActivityResult get phone number as below

if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
  // Get the URI and query the content provider for the phone number
  Uri contactUri = data.getData();
  String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
  Cursor cursor = getContext().getContentResolver().query(contactUri, projection,
      null, null, null);

  // If the cursor returned is valid, get the phone number
  if (cursor != null && cursor.moveToFirst()) {
    int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    String number = cursor.getString(numberIndex);
    // Do something with the phone number
    ... 
  } 

  cursor.close();
}