You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
project_xiaomi_Notes/doc/注释/data/Contact.java

102 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* 版权所有 (c) 2010-2011MiCode 开源社区www.micode.net
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 遵循 Apache 许可证 2.0 版本(以下简称“许可证”);
* you may not use this file except in compliance with the License.
* 除非遵循该许可证,否则不得使用本文件。
* You may obtain a copy of the License at
* 您可以通过以下链接获取许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 除非适用法律要求或以书面形式达成协议,否则依据本许可证分发的软件
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 均基于“按现状”基础提供,不附带任何明示或暗示的担保或条件。
* See the License for the specific language governing permissions and
* limitations under the License.
* 请仔细阅读许可证中的具体条款,了解相关权限和限制。
*/
package net.micode.notes.data;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import java.util.HashMap;
/**
* 联系人信息工具类,用于通过电话号码查询联系人名称
*/
public class Contact {
// 缓存电话号码与对应联系人名称,避免重复查询数据库
private static HashMap<String, String> sContactCache;
// 日志标签
private static final String TAG = "Contact";
// 查询联系人名称的SQL条件语句模板
// 功能匹配指定电话号码且数据类型为电话条目并通过phone_lookup表优化查询效率
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
+ " WHERE min_match = '+')"; // '+'将在运行时替换为实际最小匹配长度
/**
* 根据电话号码获取联系人名称
* @param context 上下文对象用于访问ContentResolver
* @param phoneNumber 要查询的电话号码
* @return 联系人名称未找到或异常时返回null
*/
public static String getContact(Context context, String phoneNumber) {
// 初始化缓存
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
// 优先从缓存中获取
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
// 构建完整查询条件:替换最小匹配长度参数
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 查询联系人数据库
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI, // 内容URI联系人数据
new String [] { Phone.DISPLAY_NAME }, // 返回字段:显示名称
selection, // 查询条件
new String[] { phoneNumber }, // 查询参数
null); // 排序方式(无)
// 处理查询结果
if (cursor != null && cursor.moveToFirst()) {
try {
String name = cursor.getString(0); // 获取第一列数据(显示名称)
sContactCache.put(phoneNumber, name); // 加入缓存
return name;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
} finally {
cursor.close(); // 确保关闭Cursor释放资源
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
}
}
}