Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting phone number by type in Android

Tags:

android

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();
}
like image 489
user778935 Avatar asked Jul 05 '11 08:07

user778935


1 Answers

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;
}
like image 90
Michael Alan Huff Avatar answered Oct 04 '22 21:10

Michael Alan Huff