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

Android联系人筛选:加载带0878/0877/0817前缀的号码

筛选前缀为"0878"、"0877"、"0817"的联系人号码方案

Got it, let's tweak your code to filter only the contacts with those specific prefixes. First, I noticed your current code does two separate database queries (one for lookup keys, then another for each key's phone number) — we can optimize this into a single query to boost performance, while adding the prefix check logic.

Here's the revised code:

private void loadFilteredContacts() {
    ContentResolver cr = applicationContext.getContentResolver();
    // 只查询我们需要的字段:号码(如果需要姓名可以添加DISPLAY_NAME)
    String[] PROJECTION = {
        ContactsContract.CommonDataKinds.Phone.NUMBER
    };

    Cursor cursor = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        PROJECTION,
        null,
        null,
        null
    );

    // 防止Cursor为空导致空指针
    if (cursor != null) {
        while (cursor.moveToNext()) {
            String rawPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String formattedPhone = Util.formatMSISDN(rawPhone);
            
            // 只保留格式化后以目标前缀开头的号码
            if (formattedPhone != null) {
                boolean matchesPrefix = formattedPhone.startsWith("0878") 
                                      || formattedPhone.startsWith("0877") 
                                      || formattedPhone.startsWith("0817");
                if (matchesPrefix) {
                    contactsSet.add(formattedPhone);
                }
            }
        }
        cursor.close();
    }
}

Key changes explained:

  • Combined database queries: Your original code fetched lookup keys first, then queried each key individually. This version pulls all phone numbers in one go, cutting down on unnecessary I/O operations and making the code faster.
  • Prefix validation: After formatting the number with your existing Util.formatMSISDN method, we check if it starts with any of your target prefixes using startsWith(). This ensures we only add matching numbers to contactsSet.
  • Null safety: Added a check for cursor != null to avoid potential crashes if the query returns no results.

Edge case handling:

If your Util.formatMSISDN method might leave country codes (like +86 or 86) at the start of the number, you'll want to strip those first before checking the prefix. For example:

if (formattedPhone != null) {
    // Remove common country code prefixes before checking
    String strippedPhone = formattedPhone.replace("+86", "").replaceFirst("^86", "");
    boolean matchesPrefix = strippedPhone.startsWith("0878") 
                          || strippedPhone.startsWith("0877") 
                          || strippedPhone.startsWith("0817");
    if (matchesPrefix) {
        contactsSet.add(formattedPhone);
    }
}

Quick reminder:

Make sure your app has the READ_CONTACTS permission — on Android 6.0+ (API level 23), you'll need to request this dynamically from the user before running the contact query.

内容的提问来源于stack exchange,提问作者asqa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:41:43