I want to retrieve the phone numbers of selected contact based on type. I want to print the phone number type and associated phone number.
I could display the phone numbers of selected contact but not able to differentiate the type.
Below is the sample code I used:
if (Integer.parseInt(cursor.getString(
  cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    Cursor phoneCursor = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{contactId,}, null
    );
    while (phoneCursor.moveToNext()) {
        // Do something with phones
        System.out.println("phone numbers :"
         + phoneCursor.getString(
            phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
         )
        );  
    }
    phoneCursor.close();
}
                Just in case you don't want to do it yourself here is a typed list with all the main types that android allows.
String sType = "";
switch (type) {
case Phone.TYPE_HOME:
    sType = "Home";
    break;
case Phone.TYPE_MOBILE:
    sType = "Mobile";
    break;
case Phone.TYPE_WORK:
    sType = "Work";
    break;
case Phone.TYPE_FAX_HOME:
    sType = "Home Fax";
    break;
case Phone.TYPE_FAX_WORK:
    sType = "Work Fax";
    break;
case Phone.TYPE_MAIN:
    sType = "Main";
    break;
case Phone.TYPE_OTHER:
    sType = "Other";
    break;
case Phone.TYPE_CUSTOM:
    sType = "Custom";
    break;
case Phone.TYPE_PAGER:
    sType = "Pager";
    break;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With