Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Contact Picker with only Phone Numbers

Tags:

I found on SO that to launch a filtered version of contact picker (which only shows contacts that have phone numbers), I can just use this:

Intent pickContactIntent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI );
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, CONTACT_PICKER_RESULT);

So this works. I'm just trying to figure out how to retrieve the name and phone number of the selected contact now, within the onActivityResult method:

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     // what goes here...
}

I've tried a number of different things inside onActivityResult, but the queries don't return the number.

like image 440
user1202422 Avatar asked Sep 01 '13 14:09

user1202422


People also ask

How do I select contacts on Android?

Contact Picker is a feature in android that a developer can use to ask users to select a particular contact. The developer can then query details related to the selected contact and perform the required action. A sample video is given below to get an idea about what we are going to do in this article.

What is contact picker API?

Available in Chrome 80 on Android, the Contact Picker API is an on-demand API that allows users to select entries from their contact list and share limited details of the selected entries with a website.


1 Answers

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);  
startActivityForResult(intent, 1);

String phoneNo = null;
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

if (cursor.moveToFirst()) {
    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    phoneNo = cursor.getString(phoneIndex);
}

curosr.close();
like image 86
Akash Yadav Avatar answered Oct 03 '22 23:10

Akash Yadav