gpt4 book ai didi

java - 如何让用户从联系人中选择电话号码,然后获取电话号码、名字和姓氏

转载 作者:太空狗 更新时间:2023-10-29 14:16:50 24 4
gpt4 key购买 nike

到目前为止我所做的是:

点击某个按钮时:

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();
}
}
}
}
}

关于java - 如何让用户从联系人中选择电话号码,然后获取电话号码、名字和姓氏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com