pull/2/head
liuyitao 1 year ago
parent 8912bcb569
commit 1fb45b47ad

@ -1 +0,0 @@
Subproject commit d0f07083988297d87c7e6d75fb1c1916d65c5d8f

@ -25,9 +25,17 @@ import android.util.Log;
import java.util.HashMap;
public class Contact { //联系人
private static HashMap<String, String> sContactCache; // sContactCache 是一个静态的 HashMap用于缓存联系人信息用于缓存联系人信息的HashMap
private static final String TAG = "Contact"; // TAG 日志标签
/*
*:
*/
/*
*
* */
public class Contact {//联系人
private static HashMap<String, String> sContactCache;// sContactCache 是一个静态的 HashMap用于缓存联系人信息用于缓存联系人信息的HashMap
private static final String TAG = "Contact";// TAG 日志标签
// 定义字符串CALLER_ID_SELECTION
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
@ -35,43 +43,43 @@ public class Contact {
+ " AND " + Data.RAW_CONTACT_ID + " IN "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
+ " WHERE min_match = '+')"; // 定义查询条件的字符串
+ " WHERE min_match = '+')";// 定义查询条件的字符串
public static String getContact(Context context, String phoneNumber) { //用于获取联系人信息。它接受一个上下文参数和一个电话号码参数,并返回与该电话号码对应的联系人名字
if(sContactCache == null) { // 如果联系人缓存为空
sContactCache = new HashMap<String, String>(); // 初始化联系人缓存
public static String getContact(Context context, String phoneNumber) {//用于获取联系人信息。它接受一个上下文参数和一个电话号码参数,并返回与该电话号码对应的联系人名字
if(sContactCache == null) {// 如果联系人缓存为空
sContactCache = new HashMap<String, String>();// 初始化联系人缓存
}
/* 查找HashMap中是否已有phoneNumber信息 */
if(sContactCache.containsKey(phoneNumber)) { // 如果在缓存中找到了电话号码对应的联系人信息
return sContactCache.get(phoneNumber); // 直接从缓存中返回联系人信息
if(sContactCache.containsKey(phoneNumber)) {// 如果在缓存中找到了电话号码对应的联系人信息
return sContactCache.get(phoneNumber);// 直接从缓存中返回联系人信息
}
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //PhoneNumberUtils.toCallerIDMinMatch() 方法将电话号码转换为最小匹配模式,构建带有电话号码的查询条件
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));//PhoneNumberUtils.toCallerIDMinMatch() 方法将电话号码转换为最小匹配模式,构建带有电话号码的查询条件
Cursor cursor = context.getContentResolver().query( // 查找数据库中phoneNumber的信息
Data.CONTENT_URI, // 联系人数据的URI
new String [] { Phone.DISPLAY_NAME }, // 要查询的字段,这里是联系人名字
selection, // 查询条件
new String[] { phoneNumber }, // 查询条件中的占位符对应的值
null); // 不指定排序
Cursor cursor = context.getContentResolver().query(// 查找数据库中phoneNumber的信息
Data.CONTENT_URI,// 联系人数据的URI
new String [] { Phone.DISPLAY_NAME },// 要查询的字段,这里是联系人名字
selection,// 查询条件
new String[] { phoneNumber },// 查询条件中的占位符对应的值
null);// 不指定排序
if (cursor != null && cursor.moveToFirst()) { // 判定查询结果如果查询结果不为空且移动到第一条记录moveToFirst()返回第一条记录)
try { // 找到相关信息
String name = cursor.getString(0); // 获取联系人名字
sContactCache.put(phoneNumber, name); // 将联系人名字放入缓存
return name; // 返回联系人名字
if (cursor != null && cursor.moveToFirst()) {// 判定查询结果如果查询结果不为空且移动到第一条记录moveToFirst()返回第一条记录)
try {// 找到相关信息
String name = cursor.getString(0);// 获取联系人名字
sContactCache.put(phoneNumber, name);// 将联系人名字放入缓存
return name;// 返回联系人名字
} catch (IndexOutOfBoundsException e) { // 捕获IndexOutOfBoundsException异常
Log.e(TAG, " Cursor get string error " + e.toString()); // 记录异常日志
return null; // 返回null
} catch (IndexOutOfBoundsException e) {// 捕获IndexOutOfBoundsException异常
Log.e(TAG, " Cursor get string error " + e.toString());// 记录异常日志
return null;// 返回null
} finally {
cursor.close(); // 关闭查询结果的Cursor
cursor.close();// 关闭查询结果的Cursor
}
// 未找到相关信息
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber); // 记录未找到联系人信息的日志
Log.d(TAG, "No contact matched with number:" + phoneNumber);// 记录未找到联系人信息的日志
return null;
}
}

Loading…
Cancel
Save