0

I've been searching and searching on how to grab multiple phone numbers (ie Home, Cell, Work) from a contact but I am Stumped. I'll add the code I'm using below. I hope some one can help. Thanks Jeff

    case CONTACT_PICKER_RESULT:
            Log.w("+DEBUG_TAG+","Got the Info");

            //handle contact results
            Cursor cursor = null;
            String number = "";
            String number2 = "";
            try{
            Uri result = data.getData();                    
            //get the content id
            String id = result.getLastPathSegment();

            //ask for the phone number
            cursor = getContentResolver().query(Phone.CONTENT_URI,
                    null, Phone.CONTACT_ID + "=?", new String[] {id},
                    null);
            int phoneIdx = cursor.getColumnIndex(Phone.DATA);

            //take the phone number
            if(cursor.moveToFirst()){
                number = cursor.getString(phoneIdx);
                Log.v("+DEBUG_TAG+","Got number " + number);
            }else if(cursor.moveToNext()){
                number2 = cursor.getString(phoneIdx);
                Log.v("+DEBUG_TAG+","GOT NumbEr2 "+ number2);
            }
            else{
                Log.e("+DEBUG_TAG","FAILED TO GET NUMBER!");
            }
            }

            finally{
                if(cursor != null){
                    cursor.close();
                }
                EditText phNumberEditText = (EditText) findViewById(R.id.number1);
                phNumberEditText.setText(number);
                if (number.length() == 0){
                    Toast.makeText(this, "No Phone Number For This Contact",
                            Toast.LENGTH_LONG).show();
            }
  • **barmaley**, what are the values of the strings R.string.home_phone, R.string.mobile_phone, R.string.work_phone...? – iosDev Nov 02 '11 at 11:13
  • try this links it ll help u http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/ – kannappan Jun 22 '11 at 04:25

1 Answers1

0

Use this kind of code:

public void readContacts(Context context)
{
    String contactId, hasPhone, phoneNumber;
    ContentResolver cr=context.getContentResolver();
    Cursor phones, cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    while (cc.moveToNext())
    {
        contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID));
        hasPhone = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        int nameFieldColumnIndex = cc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
        String contactName = cc.getString(nameFieldColumnIndex);
        Log.v(TAG, "Contact id="+contactId+" name="+contactName);
        if (Integer.parseInt(hasPhone)==1)
        {
            // You know it has a number so now query it like this
            phones = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
            while (phones.moveToNext())
            {
                phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                String label=getPhoneLabel(context, phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)),
                        phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
                Log.v(TAG, "Phone"+phoneNumber+" with label="+label);
            }
            phones.close();
        }
    }
    cc.close();
}

private String getPhoneLabel(Context context, int type, String label)
{
    String s;
    switch(type)
    {
        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
            s = context.getString(R.string.home_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
            s = context.getString(R.string.mobile_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
            s = context.getString(R.string.work_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
            s = context.getString(R.string.fax_work_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
            s = context.getString(R.string.fax_home_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
            s = context.getString(R.string.pager_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
            s = context.getString(R.string.other_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK:
            s = context.getString(R.string.callback_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_CAR:
            s = context.getString(R.string.car_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN:
            s = context.getString(R.string.company_main_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN:
            s = context.getString(R.string.isdn_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
            s = context.getString(R.string.main_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX:
            s = context.getString(R.string.other_fax_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO:
            s = context.getString(R.string.radio_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX:
            s = context.getString(R.string.telex_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD:
            s = context.getString(R.string.tty_tdd_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
            s = context.getString(R.string.work_mobile_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER:
            s = context.getString(R.string.work_pager_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT:
            s = context.getString(R.string.assistant_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_MMS:
            s = context.getString(R.string.mms_phone);
            break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
            if(label == null)
                s = context.getString(R.string.phone);
            else
                s = label;
            break;
        default:
            s = context.getString(R.string.phone);
    }
    return s;
}
Barmaley
  • 16,638
  • 18
  • 73
  • 146