cnz 4 months ago
parent 625d4a4e60
commit e2f2ffd2a1

@ -15,7 +15,7 @@
*/
package net.micode.notes.data; //导入包
// 导入所需的Android类和接口
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
@ -23,50 +23,77 @@ import android.provider.ContactsContract.Data;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import java.util.HashMap;
import java.util.HashMap; // 哈希映射,用于实现缓存
/**
*
*
*/
public class Contact { //创建类
private static HashMap<String, String> sContactCache;
private static final String TAG = "Contact";
private static HashMap<String, String> sContactCache; // 静态缓存HashMap使用volatile保证多线程可见性
private static final String TAG = "Contact"; // 日志标签,用于调试
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
/**
* SQL
*
* 1. PHONE_NUMBERS_EQUAL
* 2. MIMETYPE
* 3. 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 = '+')";
+ " AND " + Data.RAW_CONTACT_ID + " IN " // 关联raw_contact_id
+ "(SELECT raw_contact_id " // 子查询开始
+ " FROM phone_lookup" // 系统电话索引表
+ " WHERE min_match = '+')"; // 占位符将被替换
/**
*
* @param context
* @param phoneNumber
* @return null
* @throws IllegalArgumentException 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);
return sContactCache.get(phoneNumber);// 返回缓存值
}
// 动态生成查询条件:设置最小匹配长度
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME },
selection,
new String[] { phoneNumber },
null);
if (cursor != null && cursor.moveToFirst()) {
// 执行内容提供者查询:
// URI: Data.CONTENT_URI - 系统联系人数据URI
// 投影: 只查询DISPLAY_NAME列节省资源
// 参数: 将phoneNumber作为selectionArgs传入
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);
String name = cursor.getString(0);// 获取第一列索引0的数据
sContactCache.put(phoneNumber, name); // 更新缓存
return name;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
} finally {
} finally {// 确保关闭游标(避免内存泄漏)
cursor.close();
}
} else {
} else { // 未找到匹配联系人
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
}

Loading…
Cancel
Save