Read Multiple Phone Numbers From Android Contacts List Programmatically

Read Multiple Phone Numbers From Android Contacts List Programmatically

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

It is very easy to retrieve phone numbers in Android. This is how you can read multiple phone numbers from Android Contacts List programmatically. If a contact has multiple phone numbers, then You can retrieve all phone numbers and other details using Android’s inbuilt classes (Cursor and ContactsContract ) in Android. 

Screenshot-

Below is the code snippet for reading all contact details from your Android device.

Step 1: Add below packages in your Activity class or from the class where you want to read all contacts.

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;

Step 2:

Add below permission in your application’s Manifest file (AndroidManifest.xml).

<uses-permission android:name=”android.permission.READ_CONTACTS”></uses-permission>

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Step 3:

Add the below function (Copy and paste) below code snippet into your in your Activity class or from the class where you want to read all contacts. You can read inline comments inside the code. I have added details code description inside the code snippet below.

public void ReadPhoneContacts(Context cntx) //This Context parameter is nothing but your Activity class’s Context
{
Cursor cursor = cntx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list
if (contactsCount > 0)
{
while(cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
//the below cursor will give you details for multiple contacts
Cursor pCursor = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +” = ?”,
new String[]{id}, null);
// continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list
while (pCursor.moveToNext())
{
int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
//String isStarred = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//you will get all phone numbers according to it’s type as below switch case.
//Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want.
switch (phoneType)
{
case Phone.TYPE_MOBILE:
Log.e(contactName + “: TYPE_MOBILE”, ” ” + phoneNo);
break;
case Phone.TYPE_HOME:
Log.e(contactName + “: TYPE_HOME”, ” ” + phoneNo);
break;
case Phone.TYPE_WORK:
Log.e(contactName + “: TYPE_WORK”, ” ” + phoneNo);
break;
case Phone.TYPE_WORK_MOBILE:
Log.e(contactName + “: TYPE_WORK_MOBILE”, ” ” + phoneNo);
break;
case Phone.TYPE_OTHER:
Log.e(contactName + “: TYPE_OTHER”, ” ” + phoneNo);
break;
default:
break;
}
}
pCursor.close();
}
}
cursor.close();
}
}

Case 3: You just need to call above function from where you want to read phone numbers from the contacts list.

//Call this function from your Activity Class
//Below this pointer parameter is your Activity class’s this pointer
//If you want to call below function from other classes or from a service,
//then get the Context of your application or Activity and pass it to function
//ReadPhoneContacts(cntx);

PhoneContacts phoneContacts = new PhoneContacts(service);
phoneContacts.ReadPhoneContacts(cntx);

Code Description:

  • The parameter ‘Context cntx’  in the function ReadPhoneContacts(Context cntx) is nothing but the this pointer  of your Acticity class or application Context.
  • There are 2 while loops in above code, First while loop is for the Cursor (cursorvariable in the above code), which moves exactly once for each contact in the Android contacts list. Second while loop moves exactly once for each phone number associated with the contact name (contactName variable in the above code) using the second cursor (pCursor variable in the above code).
  • You can find the Contact Name in the variable(contactName variable in the above code). Then you will go into all phone numbers, which are associated with the Contact Name  (contactName variable in the above code).
  • In the Switch Case above (switch (phoneType) in the above code snippet), Each case having phone type like (TYPE_MOBILE, TYPE_HOME ect) in the switch case. In the switch case, you can find the variable phoneNo , which will provide the phone number for each phone type. Logs.e() function will print the numbers in DDMS if your phone/emulator is connected to your Eclipse editor. Else you can use the variable  phoneNo as per your requirement.

I hope this code snippet will help you at it’s best. If you have any concern or queries, then let me know. I will try to resolve your queries.

You might like to read about the complete tutorial for how to retrieve contacts and  then display in a ListView in Android.

Leave a Reply

Your email address will not be published. Required fields are marked *