我已經開始接觸拾取活動我如何獲得名字和姓從聯繫人選擇器
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, enumRequestCode.PICK_CONTACT.ordinal());
然後在onActivityResult我有這樣的代碼來獲取電話號碼(而DISPLAY_NAME,但說不好,不夠...我需要名字和姓氏能夠支持不同國家的投入):
// String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
// String surName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_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();
}
我已經試過
但這並沒有奏效。
我已經看到了這一個,但它並沒有真正幫助我很多: How to get the first name and last name from Android contacts?
任何人都可以幫助我嗎?
在此先感謝!
呃,我已經得到了電話號碼,但是我需要名字和姓氏。它看起來不像你提供了什麼? – Ikky
解析電子名片數據 vcard = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.CONTENT_VCARD_TYPE)); http://ru.wikipedia.org/wiki/VCard – alezhka