Android → Android – Open Contacts Activity and Return Chosen Contact
December 30th, 2009 by Ryan Alford
I was looking for a way to open the Contacts Activity, chose a Contact, then have that Contact returned to my application. I found a number of code snippets, but they all used depreciated API calls. So I decided to make this post with the updated API calls for Android 2.0.
First, you will need to add this permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Next, add this constant as a class level variable:
private static final int PICK_CONTACT = 3;
Now, you will add the code to open the Contacts Activity. This is done by using an Intent:
// I did this from a button click
public void btnAddContacts_Click(View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
Now, you will override the “onActivityResult” activity method, and get the Contact information:
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()){
// other data is available for the Contact. I have decided
// to only get the name of the Contact.
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}
And that is all you need to do.
January 5th, 2010 at 7:48 AM
Hi. What do you mean by “…have that Contact returned to my application.”?
January 5th, 2010 at 8:54 AM
In the code above, it will open the “Contacts” application where a user can select a Contact. When the selection is made, the “Contacts” application will close and return an intent containing data about the Contact that was chosen. In my code above, I get the name of the Contact that was chosen and display it in a Toast.
This is all done from my application. So on the click of a button in my application, I will open the Contacts application, the user will select a Contact, and I will be able to retrieve the Contact that the user chose and use that information in my application.
January 12th, 2010 at 9:29 AM
Hi. How do I retrieve the phone number?
January 12th, 2010 at 10:20 AM
@Daf,
Here is a link to a good example of getting the phone number of a specific contact.
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
The phone numbers are stored in a different table than the other contact information. Therefore, you are going to have to make a separate query to get those.
January 12th, 2010 at 11:13 AM
@Ryan
Thanks!
March 8th, 2010 at 5:41 PM
eclipsed4utoo.com, hoq do you do it?
September 2nd, 2010 at 3:06 AM
This code is not working inside GroupActivity and showing LogCat status
09-02 12:26:52.846: WARN/ActivityManager(64): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.PICK dat=content://com.android.contacts/contacts cmp=com.android.contacts/.ContactsListActivity }
Any suggetion?
September 3rd, 2010 at 7:24 AM
Thanks
It’s working perfectly.
September 12th, 2010 at 2:29 AM
This list of Contacts that comes up only lets u pick a contact. Is there any way I can also allow the user to create a new contact while displaying this Contact List. Or is there no way to do that. Also, suppose my user simply wants to send an sms to a person who is not there in his Contact list. So, is there a way i can put a “None” button or item in the list so the user can proceed without selecting a contact from this list. Thanks for your time.
September 20th, 2011 at 2:33 PM
hello there,
well in my application i need to access contacts based on a particular substring and store the retrieved contacts (all the contacts after the filtering process) into a string (only name and phone number).. could you show me a way as to how i can achieve this??
September 30th, 2011 at 5:41 PM
granda ciona de antosula y entra con aratirio aeriameg. milóis a dindesumo y prertisti gancosi con codondo caspetr!
November 3rd, 2011 at 7:20 PM
I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to create my own blog and would like to know where u got this from. kudos
December 10th, 2011 at 11:41 AM
buy best online online shopping
January 23rd, 2012 at 2:43 PM
Nyc man ..love it … code 100% working …
January 31st, 2012 at 12:16 PM
could you do same for the call logs, I mean select a contact from call log activity