Android 通讯录号码匹配规则 SQL

简介: Android 通讯录号码匹配规则 SQL

当你在系统通讯录存储8位号码联系人后,呼出时在刚刚存储8位号码前任意补足3位数,


查询到的联系人名称将被模糊匹配为8位号对应名字。举个例子,将 87354321 存储为 哈哈哈,


拨号盘拨打 01087354321,通话记录和拨号界面都会显示 哈哈哈。要想解决这个问题需要了解


系统联系人查询规则,可参考 Android6.0来电号码电话薄名称匹配


整体看下来后发现就是查询 Provider,在 packages/apps/Dialer/java/com/android/incallui/CallerInfoAsyncQuery.java


中调用可传递 selection,不想模糊匹配那直接修改 selection 将 number=实际呼出号码,经过验证发现没有用。


继续往下寻找到 packages/providers/ContactsProvider/src/com/android/providers/contacts/ContactsProvider2.java

            case PHONE_LOOKUP: {
                // Phone lookup cannot be combined with a selection
                selection = null;
                selectionArgs = null;

selection 被置为 null 了,难怪不生效。打印实际查询SQL语句如下

near “number”: syntax error (code 1 SQLITE_ERROR): , while compiling:
SELECT
contacts_view._id AS contact_id, contacts_view.display_name AS display_name, contacts_view.lookup AS lookup, data1 AS number, data4 AS normalized_number, data3 AS label, data2 AS type, contacts_view.photo_uri AS photo_uri, contacts_view.custom_ringtone AS custom_ringtone, contacts_view.send_to_voicemail AS send_to_voicemail
FROM raw_contacts
JOIN view_contacts contacts_view ON (contacts_view._id = raw_contacts.contact_id),
(SELECT data_id, normalized_number, length(normalized_number) as len FROM phone_lookup WHERE (phone_lookup.min_match = ‘1234537’)) AS lookup,
data LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id)
LEFT OUTER JOIN agg_presence ON (contact_id = agg_presence.presence_contact_id)
WHERE (lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id
AND ( lookup.len <= 11 AND substr(‘03087354321’,11 - lookup.len + 1) = lookup.normalized_number
OR ( lookup.len > 11 AND substr(lookup.normalized_number,lookup.len + 1 - 11) = ‘03087354321’)
)number=?)
ORDER BY length(lookup.normalized_number) DESC
WHERE (lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id
AND ( lookup.len <= 11 AND substr(‘03087354321’,11 - lookup.len + 1) = lookup.normalized_number
OR ( lookup.len > 11 AND substr(lookup.normalized_number,lookup.len + 1 - 11) = ‘03087354321’))) AND ( where number = ‘0308 7354 321’)
ORDER BY length(lookup.normalized_number) DESC
SELECT contacts_view._id AS contact_id, contacts_view.display_name AS display_name, contacts_view.lookup AS lookup, data1 AS number, data4 AS normalized_number, data3 AS label, data2 AS type, contacts_view.photo_uri AS photo_uri, contacts_view.custom_ringtone AS custom_ringtone, contacts_view.send_to_voicemail AS send_to_voicemail FROM raw_contacts JOIN view_contacts as contacts_view ON (contacts_view._id = raw_contacts.contact_id) JOIN (SELECT data_id,normalized_number FROM phone_lookup WHERE (phone_lookup.min_match = ‘1234537’))
AS lookup ON lookup.data_id=data._id JOIN data ON data.raw_contact_id=raw_contacts._id LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) LEFT OUTER JOIN agg_presence ON (contact_id = agg_presence.presence_contact_id)
WHERE (AND ( lookup.normalized_number = ‘03087354321’)PHONE_NUMBERS_EQUAL(data.data1, ‘0308 7354 321’, 0))
ORDER BY length(lookup.normalized_number) DESC
SELECT contacts_view._id AS contact_id, contacts_view.display_name AS display_name, contacts_view.lookup AS lookup, data1 AS number, data4 AS normalized_number, data3 AS label, data2 AS type, contacts_view.photo_uri AS photo_uri, contacts_view.custom_ringtone AS custom_ringtone, contacts_view.send_to_voicemail AS send_to_voicemail FROM raw_contacts JOIN view_contacts as contacts_view ON (contacts_view._id = raw_contacts.contact_id) JOIN (SELECT data_id,normalized_number FROM phone_lookup WHERE (phone_lookup.min_match = ‘1234537’)) AS lookup ON lookup.data_id=data._id JOIN data ON data.raw_contact_id=raw_contacts._id LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) LEFT OUTER JOIN agg_presence ON (contact_id = agg_presence.presence_contact_id)
WHERE ( ( lookup.normalized_number = ‘03087354321’) ANDPHONE_NUMBERS_EQUAL(data.data1, ‘0308 7354 321’, 0))
ORDER BY length(lookup.normalized_number) Desc


着实很复杂,最终修改如下

         // Peek at the results of the first query (which attempts to use fully
         // normalized and internationalized numbers for comparison).  If no results
         // were returned, fall back to using the SQLite function
         // phone_number_compare_loose.
+                qb.appendWhere("AND ( lookup.normalized_number = '"+normalizedNumber+"')");
         qb.setStrict(true);
         boolean foundResult = false;
         Cursor cursor = doQuery(db, qb, projectionWithNumber, selection, selectionArgs,
             sortOrder, groupBy, null, limit, cancellationSignal);
        .........
         // Use the fall-back lookup method.
         qb = new SQLiteQueryBuilder();
         qb.setProjectionMap(sPhoneLookupProjectionMap);
+                qb.appendWhere(" (lookup.normalized_number = '"+normalizedNumber+"') AND ");
         qb.setStrict(true);
         // use the raw number instead of the normalized number because
         // phone_number_compare_loose in SQLite works only with non-normalized
         // numbers
         mDbHelper.get().buildFallbackPhoneLookupAndContactQuery(qb, number);              


目录
相关文章
|
2月前
|
SQL 安全 网络安全
SQL安装程序规则错误解决方案
在安装SQL Server时,遇到安装程序规则错误是一个比较常见的问题
|
2月前
|
SQL 安全 Windows
SQL安装程序规则错误解析与解决方案
在安装SQL Server时,用户可能会遇到安装程序规则错误的问题,这些错误通常与系统配置、权限设置、依赖项缺失或版本不兼容等因素有关
|
4月前
|
Android开发
android基础:Intents 和 intent-filter 的匹配规则
这篇文章详细解释了Android开发中Intent和<intent-filter>的匹配规则,包括Action、Category和Data的解析规则以及如何通过这些规则匹配隐式Intent。
140 1
|
6月前
|
SQL 存储 关系型数据库
MySQL数据库进阶第二篇(索引,SQL性能分析,使用规则)
MySQL数据库进阶第二篇(索引,SQL性能分析,使用规则)
|
7月前
|
Android开发
【苹果安卓通用】xlsx 和 vCard 文件转换器,txt转vCard文件格式,CSV转 vCard格式,如何批量号码导入手机通讯录,一篇文章说全
本文介绍了如何快速将批量号码导入手机通讯录,适用于企业客户管理、营销团队、活动组织、团队协作和新员工入职等场景。步骤包括:1) 下载软件,提供腾讯云盘和百度网盘链接;2) 打开软件,复制粘贴号码并进行加载预览和制作文件;3) 将制作好的文件通过QQ或微信发送至手机,然后按苹果、安卓或鸿蒙系统的指示导入。整个过程简便快捷,可在1分钟内完成。
169 6
|
7月前
|
Android开发
【通讯录教程】苹果安卓鸿蒙系统通用,如何大批量导入手机号码到手机的通讯录,下面教你方法,只需1分钟搞定几万个号码的导入手机电话本
该文介绍了一种快速批量导入手机通讯录的方法,适用于处理大量手机号的需求,如微商管理、客户资料整理等。在QQ同步助手开始收费后,提供了免费的替代方案。步骤包括:下载批量导入软件(链接提供腾讯云盘和百度网盘地址),清空通讯录(非必需),制作符合格式的通讯录文件,并按操作系统(苹果、安卓或鸿蒙)进行导入。整个过程只需1分钟,简便快捷。
568 2
|
7月前
|
SQL 机器学习/深度学习 算法
SQL SERVER ANALYSIS SERVICES决策树、聚类、关联规则挖掘分析电商购物网站的用户行为数据
SQL SERVER ANALYSIS SERVICES决策树、聚类、关联规则挖掘分析电商购物网站的用户行为数据
|
SQL 数据处理 数据库
时序数据库 TDengine SQL 查询语法规则汇总,官方教程奉上!
TDengine 采用 SQL 作为查询语言,本文将就部分查询细则做分析。
562 1
|
7月前
|
Android开发
Android通讯录开发之通讯录联系人搜索功能最新实现
Android通讯录开发之通讯录联系人搜索功能最新实现