从 Android 联系人中获取多个号码

发布于 2024-11-16 16:38:05 字数 1660 浏览 2 评论 0原文

我一直在搜索和搜索如何从联系人处获取多个电话号码(即家庭、手机、工作),但我被难住了。我将在下面添加我正在使用的代码。我希望有人可以提供帮助。谢谢 杰夫

    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();
            }

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();
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-11-23 16:38:05

使用这种代码:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文