,如果你想获得所有相关的联系人,则电话号码:
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