2011-07-05 103 views
4

我想根據類型檢索選定聯繫人的電話號碼。我想打印電話號碼類型和關聯的電話號碼。在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

爲寵物這個鏈接可以嘗試

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

以防萬一你不希望自己在這裏做是一個包含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; 
} 
+0

對於Phone.TYPE_CUSTOM,你可能希望S型設置爲ContactsContract.CommonDataKinds.Phone.LABEL的價值 – d2vid

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