我已经开始了联系人选择器活动
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, enumRequestCode.PICK_CONTACT.ordinal());
然后在onActivityResult中我有这个代码来获取电话号码(和display_name,但这不好,足够......我需要名字和姓氏才能支持不同的国家/地区输入):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(enumRequestCode.PICK_CONTACT.ordinal() == requestCode){
if (resultCode == Activity.RESULT_OK) {
Cursor cursor = managedQuery(data.getData(), null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = "";
int hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String firstName = "";
String surName = "";
if (hasPhone >= 1){
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
phones.moveToNext();
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phones.close();
}
我试过了:
// String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
// String surName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
但这并不是很好。
我见过这个,但它并没有真正帮助我很多: How to get the first name and last name from Android contacts?
任何人都可以帮我这个吗?
提前感谢!
答案 0 :(得分:0)
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
if(phones.getCount() > 1) {
ArrayList<String> p = new ArrayList<String>();
while (phones.moveToNext()) {
p.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
final CharSequence[] items = p.toArray(new CharSequence[p.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(newContact.this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
phoneNumber = (String) items[item];
showContentSettings();
}
});
AlertDialog alert = builder.create();
alert.show();
}else {
phones.moveToFirst();
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
showContentSettings();
}
phones.close();
}else {
showDialog(DIALOG_PHONE_NOT_FOUND);
}
}
cursor.close();
Log.d(TAG,"name: " + name + "\nphoneNumber: " + phoneNumber);
答案 1 :(得分:0)
cursor = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(id)}, null);
int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
这就是我为名字和姓氏做的事情......以及MIMETYPE = StructuredName.CONTENT_ITEM_TYPE的其他形式的名字。