我想通过输入特定数字的3-4位来搜索android用户的联系人列表。我已经编写了以下方法来做同样的事情,但这只有在我写完数字的完整10位并且不小于该数字时才有效。这意味着它只与精确匹配一起工作。 例如,我想要以98965开头的数字...等等......但是当我输入完整的9896511112时,我只给了我一个匹配
public static void getContactDetails(Context context, String number, int type) {
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY,
ContactsContract.PhoneLookup.PHOTO_URI,
ContactsContract.PhoneLookup.NUMBER};
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.trim() + "%"));
Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
while (Common.nonNull(cursor) && cursor.moveToNext()) {
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY));
String mobileNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
Log.e("I am found", "Found " + number);
}
cursor.close();
}
请帮我找错。
答案 0 :(得分:0)
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(partial));
使用“ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI”代替 ContactsContract.PhoneLookup.CONTENT_FILTER_URI ,它将适合您。
以下是我的完整示例:
public static void getContactDetails(Context context, String number, int type) {
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY,
ContactsContract.PhoneLookup.PHOTO_URI,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(number.trim()));
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
while (Common.nonNull(cursor) && cursor.moveToNext()) {
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY));
String mobileNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
if (mobileNumber.length() > 10) {
mobileNumber = mobileNumber.substring(mobileNumber.length() - 10);
if (mobileNumber.startsWith(number)) {
Log.e("I am found", "I am found " + mobileNumber + displayName);
}
}
}
cursor.close();
}