gpt4 book ai didi

android - 打开联系人并获取电话号码

转载 作者:行者123 更新时间:2023-11-29 00:43:21 25 4
gpt4 key购买 nike

我试图在我的应用程序中打开我的联系人并允许用户选择联系人,然后用户将能够选择他们想要使用的电话号码。现在我拥有的方法允许我选择正确的联系人和电话号码,但是我不知道如何告诉用户电话号码是家庭电话号码还是手机号码。

如果是手机、家庭、工作等,我该如何在号码旁边提及

这就是我现在用来从联系人那里获取电话号码的方法。谢谢! enter image description here

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

最佳答案

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

希望这会有所帮助。

关于android - 打开联系人并获取电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8059136/

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