2011-11-09 54 views
2

我想在我的應用程序內部打開我的聯繫人並允許用戶選擇聯繫人,然後用戶將能夠選擇他們想要使用的電話號碼。現在我可以選擇正確的聯繫方式和電話號碼,但我不知道如何告訴用戶電話號碼是家庭電話號碼還是手機號碼。開放聯繫人和獲取電話號碼

我如何提旁邊的號碼,如果它是一個手機,家庭,工作等

這是我使用的是什麼,現在得到從聯繫人的電話號碼。謝謝! enter image description here

protected void onActivityResult(final int requestCode, int resultCode, 
     Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     if (data != null) { 
      Uri contactData = data.getData(); 

      try { 

       String id = contactData.getLastPathSegment(); 
       Cursor phoneCur = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = ?", new String[] { id }, null); 

       final ArrayList<String> phonesList = new ArrayList<String>(); 
       while (phoneCur.moveToNext()) { 
        // This would allow you get several phone addresses 
        // if the phone addresses were stored in an array 
        String phone = phoneCur 
          .getString(phoneCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
        phonesList.add(phone); 
       } 
       phoneCur.close(); 
       if (phonesList.size() == 0) { 
        Toast.makeText(this, "No Phone Number in Contact", 
          Toast.LENGTH_SHORT).show(); 
       } else if (phonesList.size() == 1) { 
        switch (requestCode) { 
        case (1): 
         edit1.setText(phonesList.get(0)); 
         break; 
        case (2): 
         edit2.setText(phonesList.get(0)); 
         break; 
        case (3): 
         edit3.setText(phonesList.get(0)); 
         break; 
        } 
       } else { 

        final String[] phonesArr = new String[phonesList.size()]; 
        for (int i = 0; i < phonesList.size(); i++) { 
         phonesArr[i] = phonesList.get(i); 
        } 

        AlertDialog.Builder dialog = new AlertDialog.Builder(
          Settings.this); 
        dialog.setTitle("Choose Phone Number"); 
        ((Builder) dialog).setItems(phonesArr, 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            String selectedEmail = phonesArr[which]; 
            switch (requestCode) { 
            case (1): 
             edit1.setText(selectedEmail); 
             break; 
            case (2): 
             edit2.setText(selectedEmail); 
             break; 
            case (3): 
             edit3.setText(selectedEmail); 
             break; 
            } 
           } 
          }).create(); 
        dialog.show(); 
       } 
      } catch (Exception e) { 
       Log.e("FILES", "Failed to get phone data", e); 
      } 
     } 
    } 

回答

2
ContentResolver contentResolver = getContentResolver(); 

    Cursor cursor = contentResolver.query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
     new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, 
      ContactsContract.CommonDataKinds.Phone.TYPE }, 
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" 
      + contactId, null, null + " DESC"); 

    if (cursor.moveToFirst()) { 
     do { 
     int phoneNumberColumn = cursor.getColumnIndex(Phone.NUMBER); 
     int phoneTypecolumn = cursor.getColumnIndex(Phone.TYPE); 

     int phoneType = Integer.parseInt(cursor 
      .getString(phoneTypecolumn)); 

     String phoneNumberType = ""; 

     if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) { 
      phoneNumberType = "Home"; 
     } else if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) { 
      phoneNumberType = "Mobile"; 
     } .....//Similarly the other types could be obtained. 

     } while (cursor.moveToNext()); 
    } 

希望這會有所幫助。

相關問題