Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick contact directly from contact picker intent

Tags:

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

like image 325
Hardik Trivedi Avatar asked Dec 26 '16 06:12

Hardik Trivedi


People also ask

What is the name of the intent action to pick a contact number?

Contact Picker is a feature in android that a developer can use to ask users to select a particular contact.

How do I select and display my phone number from a contact list on Android?

Open your Contacts app and tap the Options button (three dots), and select Contacts Manager. On the next screen, tap on Contacts to display from the menu. Next, if you only want contacts with a phone number, tap on Phone.

How do I use contact picker in flutter?

flutter_contact_picker. With this plugin a Flutter app can ask its user to select a contact from his/her address book. The information associated with the contact is returned to the app. This plugin uses the operating system's native UI for selecting contacts and does not require any special permissions from the user.


1 Answers

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();
}
like image 70
schnill Avatar answered Sep 20 '22 09:09

schnill