开发者

Getting phone number by type in 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();
}


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


as pet this link can try

http://www.vtgroup.com/#ContactsContract

        //  Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_HOME:
                    // do something with the Home number here...
                    break;
                case Phone.TYPE_MOBILE:
                    // do something with the Mobile number here...
                    break;
                case Phone.TYPE_WORK:
                    // do something with the Work number here...
                    break;
                }
        }
        phones.close();


Here the complete list:

    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;
    case Phone.TYPE_ASSISTANT:
        sType = "Assistant";
        break;
    case Phone.TYPE_CALLBACK:
        sType = "Callback";
        break;
    case Phone.TYPE_CAR:
        sType = "Car";
        break;
    case Phone.TYPE_COMPANY_MAIN:
        sType = "Company Main";
        break;
    case Phone.TYPE_ISDN:
        sType = "ISDN";
        break;
    case Phone.TYPE_MMS:
        sType = "MMS";
        break;
    case Phone.TYPE_OTHER_FAX:
        sType = "Other Fax";
        break;
    case Phone.TYPE_RADIO:
        sType = "Radio";
        break;
    case Phone.TYPE_TELEX:
        sType = "Telex";
        break;
    case Phone.TYPE_TTY_TDD:
        sType = "TTY TDD";
        break;
    case Phone.TYPE_WORK_MOBILE:
        sType = "Work Mobile";
        break;
    case Phone.TYPE_WORK_PAGER:
        sType = "Work Pager";
        break;
    }


In Kotlin:

** Note you have to pass the [contactId] to this function **

fun getPhoneTypeWithNumber(contactId: String){

    val phones: Cursor? = contentResolver.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
        arrayOf(contactId),
        null
    )
    phones?.let {
        it.moveToFirst()
        val phoneNumbers: Int = it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
        val typeLabel = it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)
       
        while (!it.isAfterLast) {
            // it will return in number
            // we have to convert into text
            val type = getPhoneType(it.getInt(typeLabel))
            println("Check Phone Number $phoneNumbers  Type ::>  $type")
            it.moveToNext()

        }
        it.close()
    }
}

Get PhoneType function:

private fun getPhoneType(type: Int): String{
    return when(type){
        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE -> {
            "Mobile"
        }
        ContactsContract.CommonDataKinds.Phone.TYPE_WORK -> {
            "Work"
        }
        ContactsContract.CommonDataKinds.Phone.TYPE_HOME -> {
            "Home"
        }
        ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME -> {
            "Fax"
        }
        ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK -> {
            "Fax"
        }
        ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX -> {
            "Fax"
        }
        else -> {
            "Other"
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜