本文介绍了从Android联系人抓住倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找,并就如何抓住多个电话号码从联系人(即家庭,小区,工作)寻找,但我难倒。我将添加我使用下面的code。我希望有人可以提供帮助。谢谢
杰夫

 情况下CONTACT_PICKER_RESULT:
            Log.w(+ DEBUG_TAG +,得到的信息);            //把手接触结果
            光标光标= NULL;
            串号=;
            串号码2 =;
            尝试{
            乌里结果= data.getData();
            //获取内容ID
            字符串ID = result.getLastPathSegment();            //索要电话号码
            光标= getContentResolver()查询(Phone.CONTENT_URI,
                    空,Phone.CONTACT_ID +=?,新的String [] {ID},
                    空值);
            INT phoneIdx = cursor.getColumnIndex(Phone.DATA);            //拿电话号码
            如果(cursor.moveToFirst()){
                数= cursor.getString(phoneIdx);
                Log.v(+ DEBUG_TAG +,拿到号码+号);
            }否则如果(cursor.moveToNext()){
                号码2 = cursor.getString(phoneIdx);
                Log.v(+ DEBUG_TAG +,GOT NUMBER2+数字2);
            }
            其他{
                Log.e(+ DEBUG_TAG,未能获得多少!);
            }
            }            最后{
                如果(指针!= NULL){
                    cursor.close();
                }
                的EditText phNumberEditText =(EditText上)findViewById(R.id.number1);
                phNumberEditText.setText(数);
                如果(number.length()== 0){
                    Toast.makeText(这一点,没有电话号码,这跟
                            Toast.LENGTH_LONG).show();
            }


解决方案

使用这种code的:

 公共无效readContacts(上下文的背景下)
{
    字符串的ContactID,hasPhone,phoneNumber的;
    ContentResolver的CR = context.getContentResolver();
    光标电话,CC = cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
    而(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);
        字符串CONTACTNAME = cc.getString(nameFieldColumnIndex);
        Log.v(TAG,接触式ID =+ +的ContactIDNAME =+联系人姓名);
        如果(的Integer.parseInt(hasPhone)== 1)
        {
            //你知道它有许多所以现在查询像这样
            手机= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    空值,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+的ContactID,NULL,NULL);
            而(phones.moveToNext())
            {
                phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                字符串标签= getPhoneLabel(背景下,phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))
                        phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
                Log.v(TAG,手机+ phoneNumber的+与标签=+标签);
            }
            phones.close();
        }
    }
    cc.close();
}私人字符串getPhoneLabel(上下文的背景下,整型,字符串标签)
{
    字符串s;
    开关(类型)
    {
        案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
            S = context.getString(R.string.home_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
            S = context.getString(R.string.mobile_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
            S = context.getString(R.string.work_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
            S = context.getString(R.string.fax_work_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
            S = context.getString(R.string.fax_home_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
            S = context.getString(R.string.pager_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
            S = context.getString(R.string.other_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK:
            S = context.getString(R.string.callback_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_CAR:
            S = context.getString(R.string.car_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN:
            S = context.getString(R.string.company_main_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_ISDN:
            S = context.getString(R.string.isdn_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
            S = context.getString(R.string.main_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX:
            S = context.getString(R.string.other_fax_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_RADIO:
            S = context.getString(R.string.radio_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_TELEX:
            S = context.getString(R.string.telex_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD:
            S = context.getString(R.string.tty_tdd_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
            S = context.getString(R.string.work_mobile_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER:
            S = context.getString(R.string.work_pager_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT:
            S = context.getString(R.string.assistant_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_MMS:
            S = context.getString(R.string.mms_phone);
            打破;
        案例ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
            如果(标签== NULL)
                S = context.getString(R.string.phone);
            其他
                S =标签;
            打破;
        默认:
            S = context.getString(R.string.phone);
    }
    返回S;
}

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. ThanksJeff

    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();
            }
解决方案

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

这篇关于从Android联系人抓住倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 05:51