IF you want to get all the phone numbers related to a contact then:
1) Use this intent to open contacts app:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
2) in onActivityResult
use following code:
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);
}
}
}
}
This will set the selected phone no in a edit text named editText. You can change this as per your need.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…