Search code examples
androidcontacts

How to get different types of mobile numbers from contacts in android?


Using the following Code:

int index3 = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); But it only gets one number and i also don't know which type of number it is. I want to get phone of type mobile , work , home etc. What would be the code for that ?


Solution

  • Figured it out you can simply use a switch statement on the TYPE and get different numbers and their type

    CODE:

           while (phoneCursor.moveToNext()) {
                    int index3 = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int type = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                    int type1 = phoneCursor.getInt(type);
    
                    switch (type1) {
                        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                            numberWork = phoneCursor.getString(index3);
                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                            numberMobile = phoneCursor.getString(index3);
                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                            numberHome = phoneCursor.getString(index3);
                            break;
    
                    }
                }