我无法使用LookUpKey
来获取某人下保存的所有联系电话。我已经在一个人(家庭,移动和工作)下保存了三个联系电话。但是,当我使用联系人的lookUpKey查询联系人时,并不会获取所有已保存的联系人。
这是我的代码
ArrayList<ContactItem> contactItems = new ArrayList<>(5);
String lookUpKey = params[1];
Uri dataUri = ContactsContract.Data.CONTENT_URI;
Cursor dataCursor = mApplication.getContentResolver().query(dataUri,
null,
ContactsContract.Data.LOOKUP_KEY + " = ?",
new String[]{lookUpKey}, null);
if (dataCursor == null || !dataCursor.moveToFirst()) return contactItems;
M.logE("cursor length = " + dataCursor.getCount());
M.logE("column count = " + dataCursor.getColumnCount());
ArrayList<String> tempContacts = new ArrayList<>();
String homePhone;
String mobilePhone;
String workPhone;
while (dataCursor.moveToNext()) {
final String data2 = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
final int contactType = dataCursor.getInt(dataCursor.getColumnIndex(ContactsContract.Data.DATA2));
M.logE("Contact type = " + contactType);
if (!data2.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
continue;
}
if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
if (TextUtils.isEmpty(mobilePhone)) continue;
mobilePhone = mobilePhone.replace("-", "");
mobilePhone = mobilePhone.replace(" ", "");
if (!tempContacts.contains(mobilePhone)) {
tempContacts.add(mobilePhone);
contactItems.add(new ContactItem(mobilePhone, "Mobile"));
}
} else if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) {
homePhone = dataCursor.getString(dataCursor
.getColumnIndex(DATA_1));
if (TextUtils.isEmpty(homePhone)) continue;
homePhone = homePhone.replace("-", "");
homePhone = homePhone.replace(" ", "");
if (!tempContacts.contains(homePhone)) {
tempContacts.add(homePhone);
contactItems.add(new ContactItem(homePhone, "Home"));
}
} else if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
workPhone = dataCursor.getString(dataCursor
.getColumnIndex(DATA_1));
if (TextUtils.isEmpty(workPhone)) continue;
workPhone = workPhone.replace("-", "");
workPhone = workPhone.replace(" ", "");
if (!tempContacts.contains(workPhone)) {
tempContacts.add(workPhone);
contactItems.add(new ContactItem(workPhone, "Work"));
}
}
}
这有什么问题?如何使用lookUpKey
获取所有详细信息?
答案 0 :(得分:0)
首先,您不会考虑“电话”类型的所有选项,除了手机/家庭/办公以外,还有其他可能性。
第二,您在该代码上有很多“继续”,每个都可能是罪魁祸首,我建议您稍微更改一下查询内容,以通过在Phone上查询来代替CONTENT_ITEM_TYPE
的第一张支票。然后将光标转储到日志中,然后手动检查您是否看到了所有希望看到的电话。
除此之外,查询看起来还可以。
String[] projection = new String[] { Phone.NUMBER, Phone.TYPE };
String selection = Phone.LOOKUP_KEY + " = ?";
String[] selectionArgs = new String[] {lookUpKey};
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
DatbaseUtils.dumpCursor(cursor);
cursor.close();