|
|
|
@ -26,9 +26,12 @@ import android.util.Log;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
public class Contact {
|
|
|
|
|
// 定义一个HashMap类型的静态变量sContactCache
|
|
|
|
|
private static HashMap<String, String> sContactCache;
|
|
|
|
|
// 定义一个字符串类型的静态变量TAG
|
|
|
|
|
private static final String TAG = "Contact";
|
|
|
|
|
|
|
|
|
|
// 定义一个字符串类型的静态变量CALLER_ID_SELECTION
|
|
|
|
|
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 +39,23 @@ public class Contact {
|
|
|
|
|
+ " FROM phone_lookup"
|
|
|
|
|
+ " WHERE min_match = '+')";
|
|
|
|
|
|
|
|
|
|
public static String getContact(Context context, String phoneNumber) {
|
|
|
|
|
public static String getContact(Context context, String phoneNumber) {
|
|
|
|
|
// 判断sContactCache是否为空
|
|
|
|
|
if(sContactCache == null) {
|
|
|
|
|
// 初始化sContactCache
|
|
|
|
|
sContactCache = new HashMap<String, String>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断sContactCache中是否包含phoneNumber
|
|
|
|
|
if(sContactCache.containsKey(phoneNumber)) {
|
|
|
|
|
// 返回sContactCache中phoneNumber对应的value
|
|
|
|
|
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 },
|
|
|
|
@ -54,10 +63,14 @@ public class Contact {
|
|
|
|
|
new String[] { phoneNumber },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
// 判断cursor是否为空
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取查询结果
|
|
|
|
|
String name = cursor.getString(0);
|
|
|
|
|
// 将查询结果存入sContactCache中
|
|
|
|
|
sContactCache.put(phoneNumber, name);
|
|
|
|
|
// 返回查询结果
|
|
|
|
|
return name;
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, " Cursor get string error " + e.toString());
|
|
|
|
@ -70,4 +83,4 @@ public class Contact {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|