,如果你想獲得所有相關的聯繫人,則電話號碼:
1)使用此意圖打開通訊錄應用程序:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
2)onActivityResult
使用下面的代碼:
if (requestCode == PICK_CONTACT) {
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) {
Helper.showToast(
this,
getString(R.string.error_no_phone_no_in_contact),
Toast.LENGTH_LONG);
} else if (phonesList.size() == 1) {
editText.setText(phonesList.get(0));
} 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(
SendSMS.this);
dialog.setTitle(R.string.choose_phone);
((Builder) dialog).setItems(phonesArr,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
String selectedEmail = phonesArr[which];
editText.setText(selectedEmail);
}
}).create();
dialog.show();
}
} catch (Exception e) {
Log.e("FILES", "Failed to get phone data", e);
}
}
}
}
這將在名爲editText的編輯文本中設置選定的電話號碼。您可以根據需要更改此設置。
請具體在您的問題。在你的「編輯」中,你是自相矛盾的。你能告訴我什麼是「一個聯繫人的所有電話號碼」和「一個聯繫人的電話號碼」的差異btw? – mudit 2011-04-05 08:50:12
對不起,如果不明確。兩者對我來說都是一樣的。我想過濾由ContactsContract.CommonDataKinds.Phone.CONTENT_URI給出的列表,以便顯示的列表僅包含附加到給定聯繫人的電話號碼。 – 2011-04-05 09:36:32