在Android中按类型获取电话号码

时间:2011-07-05 08:40:45

标签: android

我想根据类型检索所选联系人的电话号码。我想打印电话号码类型和相关的电话号码。

我可以显示所选联系人的电话号码,但无法区分类型。

以下是我使用的示例代码:

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

3 个答案:

答案 0 :(得分:7)

以防万一你不想自己动手这里是一个带有android允许的所有主要类型的打字列表。

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

答案 1 :(得分:5)

作为宠物此链接可以尝试

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

答案 2 :(得分:2)

这里有完整的清单:

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