Sqlite LIKE函数无返回结果,请求协助排查联系人号码查询问题
Let's walk through why your current code might not be returning results and how to fix it:
1. Incomplete Character Removal
Your current replaceAll only strips a small set of special characters ([-+.^:,]), but you mentioned stored numbers include spaces—and phone numbers often have other symbols like parentheses (), slashes /, or brackets [] too. Instead of listing every possible symbol, it's more reliable to remove all non-digit characters from the input:
// Remove every character that isn't a digit contactNumber = contactNumber.replaceAll("\\D", "");
This ensures your cleaned input matches the core digits of the stored number, regardless of formatting.
2. Using the Non-Standard NUMBER Column
The ContactsContract.CommonDataKinds.Phone.NUMBER column stores numbers in their raw, user-entered format, which can vary wildly. Android provides a NORMALIZED_NUMBER column (available from API level 16) that standardizes numbers into a consistent format (e.g., removing special characters, normalizing country codes). Querying against this column will give you much more reliable results:
// Use NORMALIZED_NUMBER instead of NUMBER for consistent matching String selection = ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER + " LIKE ?";
3. Adjust the LIKE Pattern (Optional but Useful)
Since normalized numbers often include a country code prefix (e.g., +1 for the US), if your input doesn't include this prefix, using a trailing % might be sufficient (instead of wrapping the entire string in %). However, wrapping in % still works if you want to match any occurrence of the digits:
contactNumber = "%" + contactNumber + "%";
4. Verify Permissions
Don't forget to ensure your app has the READ_CONTACTS permission. Without it, your query will return an empty cursor. Add this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS" />
And request runtime permission for Android 6.0 (API 23) and above.
Corrected Full Code
Putting it all together:
// Clean input: remove all non-digit characters contactNumber = contactNumber.replaceAll("\\D", ""); contactNumber = "%" + contactNumber + "%"; String strEmail = ""; String name = ""; // Use NORMALIZED_NUMBER for more reliable matching String selection = ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER + " LIKE ?"; Cursor cursor = ctx.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection, new String[]{contactNumber}, null ); // Don't forget to handle the cursor properly (check if it's not null, moveToFirst, etc.) if (cursor != null && cursor.moveToFirst()) { // Extract data here name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); // ... other fields cursor.close(); }
Additional Notes
- If you need to support API levels below 16, you'll have to stick with the
NUMBERcolumn but ensure your input cleaning covers all possible formatting characters in stored numbers. - Always close the cursor after use to avoid resource leaks.
内容的提问来源于stack exchange,提问作者Dharmesh Patel

