Pages Navigation Menu

Coding is much easier than you think

How to call Android contacts list and Display his name and Phone Number after selection?

There are three steps to this process.

1) Permissions

Add a permission to read contacts data to your application manifest.

2) Calling the Native Contact Picker

Android already has behavior built in to select contacts. This is used to select contacts for phone calls and other native apps. But it can also be used by apps like yours so you don’€™t have top build it yourself. Intents are a generic mechanism for invoking an action that the system can respond to. When the Android action code processed that Intent, it saw the reference to the Activity and invoked it directly. You don’€™t actually have to include a reference to an Activity in an Intent. You can also supply a Uri or a combination of Uri and an Action. And if you invoke the Intent, the Android action code looks for an Activity that responds to that Uri and invokes it.
 
** UPDATE: Android Complete tutorial now available here.
 
Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

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

Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACT in this example). This will cause Android to launch an Activity that’s registered to support ACTION_PICK on the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the ‘select a contact’ Activity you launched in step 2. You should check that the returned request code matches the value you’re expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter.

When you print URI , it will be like this

08-10 15:44:52.131: DEBUG/Intent Data(355): content://com.android.contacts/contacts/lookup/0r1-512D45/1

To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@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()) {

String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

// TODO Whatever you want to do with the selected contact name.

}
}
break;
}
}

4 ) Display Phone Number

There is two steps to get Phone Number :

First we will get id for the selected contact through ContentResolver passing URI .
Second we will query ContentResolver for all phone numbers using android constant “ContactsContract.CommonDataKinds.Phone.CONTENT_URI” and adding where class through previous got id.

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 contactCursor = getContentResolver().query(uri,
				new String[] { ContactsContract.Contacts._ID }, null, null,
				null);
		String id = null;
		if (contactCursor.moveToFirst()) {
			id = contactCursor.getString(contactCursor
					.getColumnIndex(ContactsContract.Contacts._ID));
		}
		contactCursor.close();
		String phoneNumber = null;
		Cursor phoneCursor = getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "= ? ",
				new String[] { id }, null);
		if (phoneCursor.moveToFirst()) {
			phoneNumber = phoneCursor
					.getString(phoneCursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
		}
		phoneCursor.close();

break;
}
}