You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sqlite LIKE函数无返回结果,请求协助排查联系人号码查询问题

Troubleshooting Your Contact Number Query Issue

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 NUMBER column 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 08:52:28