更新了data的注释

master
田艳 2 years ago
parent a52c611305
commit ffbefea38b

@ -25,49 +25,38 @@ import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
public class Contact { public static String getContact(Context context, String phoneNumber) {
private static HashMap<String, String> sContactCache; if(sContactCache == null) { //如果缓存为空则创建一个新的 HashMap
private static final String TAG = "Contact"; sContactCache = new HashMap<String, String>();
}
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);
}
String selection = CALLER_ID_SELECTION.replace("+", if(sContactCache.containsKey(phoneNumber)) { //如果缓存中已存在对应的电话号码则返回其对应的联系人姓名
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); return sContactCache.get(phoneNumber);
Cursor cursor = context.getContentResolver().query( }
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME },
selection,
new String[] { phoneNumber },
null);
if (cursor != null && cursor.moveToFirst()) { //否则根据电话号码查询联系人姓名
try { String selection = CALLER_ID_SELECTION.replace("+",
String name = cursor.getString(0); PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将 "+" 替换为待查询电话号码的最小完全匹配
sContactCache.put(phoneNumber, name); Cursor cursor = context.getContentResolver().query(
return name; Data.CONTENT_URI, //uri为ContentProvider提供的数据查询接口的位置(Data.CONTENT_URI)
} catch (IndexOutOfBoundsException e) { new String [] { Phone.DISPLAY_NAME }, //需要查询的数据项,查询联系人姓名
Log.e(TAG, " Cursor get string error " + e.toString()); selection, //查询条件
return null; new String[] { phoneNumber }, //占位符对应的实际参数
} finally { null); //排序方式为空
cursor.close();
} if (cursor != null && cursor.moveToFirst()) { //如果查询结果非空,获取查询结果中的第一条记录
} else { try {
Log.d(TAG, "No contact matched with number:" + phoneNumber); String name = cursor.getString(0); //获取联系人姓名
return null; 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