标识Contact类

pull/3/head
江一阳 2 years ago
parent cf45266af3
commit 65101b72a8

@ -25,10 +25,14 @@ import android.util.Log;
import java.util.HashMap;
// Contact类用于查询联系人信息
public class Contact {
// 用于缓存已查询到的联系人名称
private static HashMap<String, String> sContactCache;
// 日志标签
private static final String TAG = "Contact";
// 查询联系人的选择条件
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 "
@ -36,17 +40,22 @@ public class Contact {
+ " FROM phone_lookup"
+ " WHERE min_match = '+')";
// 根据电话号码获取联系人名称的方法
public static String getContact(Context context, String phoneNumber) {
// 如果缓存为空则创建一个新的HashMap
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
// 如果缓存中已存在该电话号码的联系人,则直接返回其名称
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
// 替换CALLER_ID_SELECTION中的"+"为phoneNumber的最小匹配形式
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 查询联系人信息
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME },
@ -54,20 +63,27 @@ public class Contact {
new String[] { phoneNumber },
null);
// 如果查询结果不为空且有结果,则获取联系人名称
if (cursor != null && cursor.moveToFirst()) {
try {
String name = cursor.getString(0);
// 将联系人名称存入缓存
sContactCache.put(phoneNumber, name);
// 返回联系人名称
return name;
} catch (IndexOutOfBoundsException e) {
// 获取联系人名称出错记录日志并返回null
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
} finally {
// 关闭游标
cursor.close();
}
} else {
// 查询结果为空记录日志并返回null
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
}
}
}
/*这个类主要用于根据电话号码查询联系人名称。在查询过程中会将查询到的联系人名称缓存在一个HashMap中以便于下次查询时直接从缓存中获取。*/
Loading…
Cancel
Save