更新了data的注释

master
田艳 2 years ago
parent a52c611305
commit ffbefea38b

@ -25,49 +25,38 @@ import android.util.Log;
import java.util.HashMap;
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 "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
+ " WHERE min_match = '+')";
public static String getContact(Context context, String phoneNumber) {
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
public static String getContact(Context context, String phoneNumber) {
if(sContactCache == null) { //如果缓存为空则创建一个新的 HashMap
sContactCache = new HashMap<String, String>();
}
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(sContactCache.containsKey(phoneNumber)) { //如果缓存中已存在对应的电话号码则返回其对应的联系人姓名
return sContactCache.get(phoneNumber);
}
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();
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
//否则根据电话号码查询联系人姓名
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将 "+" 替换为待查询电话号码的最小完全匹配
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI, //uri为ContentProvider提供的数据查询接口的位置(Data.CONTENT_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); //将电话号码及其联系人姓名加入 HashMap 缓存中
return name; //返回联系人姓名
} catch (IndexOutOfBoundsException e) { //处理异常
Log.e(TAG, " Cursor get string error " + e.toString());
return null; //返回null
} finally {
cursor.close(); //释放cursor
}
} else { //如果返回为空打印调试信息并返回null
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null; //返回null
}
}

Loading…
Cancel
Save