我正在尝试在我的应用内打开我的联系人并允许用户选择联系人,然后用户就可以选择他们想要使用的电话号码。现在我的方法允许我选择正确的联系人和电话号码,但我不知道如何告诉用户电话号码是家庭电话号码还是手机号码。
如果是电话,家庭,工作等,我如何在号码旁边提及
这就是我现在用来从联系人那里获取电话号码的方法。谢谢!
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);
}
}
}
答案 0 :(得分: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());
}
希望这会有所帮助。