gpt4 book ai didi

android - 阅读联系电话号码对某些人有效,对其他人无效?

转载 作者:行者123 更新时间:2023-11-29 20:59:06 26 4
gpt4 key购买 nike

我正在从 Android 的联系人列表中读取联系人姓名和电话号码。我正在成功阅读名字。但是,当我按姓名查找联系人电话号码时(在这种情况下都是唯一的,这只是一个测试),它仅适用于一个联系人,没有其他人的电话号码,并且电话号码错误一个。

这是我的 getNumbers 方法中的代码:

private String getNumber(String name){
String returnMe="";
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + getIntent().getStringExtra("name") + "'", null, null);

if(cursor.moveToFirst()){
String identifier = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier, null, null);
while(phones.moveToNext()){
returnMe = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch(type){
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
System.out.println("mobile number found"); break;
default:
System.out.println("nothing found."); break;
}
}
}
return returnMe;
}

最佳答案

你做错了几件事:

1) 第二个查询必须以 Data 为目标表,而不是电话表:

Cursor phones = contentResolver.query(ContactsContract.Data.CONTENT_URI, ...

并在 where 子句的 DATA1 列中指定您想要的电话号码:

Data.MIMETYPE = Phone.CONTENT_ITEM_TYPE

2) 您需要通过 RawContact 的 _ID 过滤结果

比较 Contact 行的 _ID 和 Phone 行的 _ID,两者相同的可能性很小:

ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier

这会将 Data.CONTACT_ID 与光标的 Contact._ID 属性进行比较

Data.CONTACT_ID + " = " + identifier

Data 上的示例javadoc 页面给出了一个更完整的示例:

查找给定联系人的给定类型的所有数据

Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contactId)}, null);

关于android - 阅读联系电话号码对某些人有效,对其他人无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727695/

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