首先,let's使得被叫爲選擇器
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
然後在onActivityResult()讓我們檢查多少數量已選定的聯繫人,如果人數> 1,則可以顯示一個對話框,選擇一個:
Uri result = data.getData();
Log.v(TAG, "Got a result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for phone numbers for the selected contact id
c = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);
if(c.getCount() > 1) { // contact has multiple phone numbers
final CharSequence[] numbers = new CharSequence[c.getCount()];
int i=0;
if(c.moveToFirst()) {
while(!c.isAfterLast()) { // for each phone number, add it to the numbers array
String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number
String number = type + ": " + c.getString(phoneIdx);
numbers[i++] = number;
c.moveToNext();
}
// build and show a simple dialog that allows the user to select a number
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.select_contact_phone_number_and_type);
builder.setItems(numbers, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
String number = (String) numbers[item];
int index = number.indexOf(":");
number = number.substring(index + 2);
loadContactInfo(number); // do something with the selected number
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity(this);
alert.show();
} else Log.w(TAG, "No results");
} else if(c.getCount() == 1) {
// contact has a single phone number, so there's no need to display a second dialog
}
但這需要我自己「挖」號碼列表。有可能讓android做到這一點嗎?我可以顯示我的聯繫人列表中的所有電話號碼列表(如果我有3個電話號碼,他會顯示John Doe的3次)。但是我可以通過這種方式使它成爲John的名字,然後Android本身爲我提供了John的所有3個數字的列表供我選擇?我知道它的要求非常高,但它在所有API版本上總是看起來「很好」 – MarioLobo
嗯,我知道,沒有內置功能可以做你想做的事,你可以選擇與選擇器的聯繫,它會返回他的所有數據,你將不得不得到所有的數字(如果有超過1),然後創建代碼選擇一個,這就是我所知道的 –
無論如何。 我會再等一天,看看有沒有人能找到解決方案。 如果不是,我會將你的答案標記爲正確的做可能的事情。 ;) – MarioLobo