[英]How to let the user choose a phone number from contacts and then get the phone number, the given name and the family name
我到現在所做的是:
點擊某個按鈕時:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
然后:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = null;
Cursor cursorStructuredName = null;
try{
cursor = getContentResolver().query(uri, null, null, null, null);
boolean hasNext = cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String phoneNumber = null;
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
if (phones.moveToFirst()) {//does not get inside the if
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
// projection
String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.PREFIX,
ContactsContract.CommonDataKinds.StructuredName.SUFFIX};
String where = ContactsContract.Data.RAW_CONTACT_ID + " =?";
String[] whereParameters = new String[]{contactId};
//Request
cursorStructuredName = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);
String displayName = null;
String givenName = null;
String middleName = null;
String familyName = null;
String prefix = null;
String suffix = null;
hasNext = cursorStructuredName.moveToFirst();
if (cursorStructuredName != null && hasNext) {//does not get here because hasNext equals false
displayName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
givenName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
middleName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
familyName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
prefix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
suffix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
}
} finally {
if (cursor != null) {
cursor.close();
}
if (cursorStructuredName != null) {
cursorStructuredName.close();
}
}
}
}
break;
}
}
}
我無法獲得姓氏,姓名等。我無法獲得所選的電話號碼。
正如我在標題中所說,我想要的是當用戶點擊某個按鈕時,將打開聯系人列表,當他選擇特定聯系人時,會顯示此聯系人電話號碼列表,然后當他選擇一個號碼時方法onAtivityResult將被調用,我將“解析”電話號碼,姓氏,姓名等。
好吧,經過更多的研究,我已經解決了我的問題。
下一個代碼允許用戶按下按鈕,然后打開聯系人應用程序,讓用戶選擇聯系人(僅顯示具有至少一個電話號碼的聯系人)。 在選擇聯系人之后,其所有號碼的列表都是打開的,讓用戶可以選擇一個(例如,在某些設備中,相同的聯系人出現的次數超過一次,每個電話號碼都有一個) )。 之后調用onActivityResult()方法,然后(如果一切正常),我將取消所選擇的電話號碼,給定名稱和所選聯系人的姓氏:
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
handleContactSelection(data);
break;
}
}
}
private void handleContactSelection(Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = null;
Cursor nameCursor = null;
try {
cursor = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
CommonDataKinds.Phone.CONTACT_ID} ,
null, null, null);
String phoneNumber = null;
String contactId = null;
if (cursor != null && cursor.moveToFirst()) {
contactId = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.CONTACT_ID));
phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
String givenName = null;///first name.
String familyName = null;//last name.
String projection[] = new String[]{ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME};
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contactId};
nameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
if(nameCursor != null && nameCursor.moveToNext()) {
givenName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
familyName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
}
doSomething(phoneNumber,givenName,familyName);
} finally {
if (cursor != null) {
cursor.close();
}
if(nameCursor != null){
nameCursor.close();
}
}
}
}
}
我相信這個會解決你的一些問題。 這基本上需要一個phoneno並查詢其聯系號。我使用它來將數據上傳到服務器。 至於電話號碼,我從收到的電話聽眾那里得到它。
String[] projection = new String[]{ ContactsContract.PhoneLookup.DISPLAY_NAME}; Uri contacturi= Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneno)); ContentResolver cr = getContentResolver(); Cursor crc= cr.query(contacturi, projection, null, null, null); if(crc.moveToNext()){ name=crc.getString(crc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); } else{ name ="Unknown"; }
您需要在crc游標上添加所有必填字段。 即家庭,電子郵件,家庭。 希望這能給你一些見解: - /
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.