|
|
@ -25,10 +25,12 @@ import android.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
|
|
public class Contact {
|
|
|
|
public class Contact {//联系人
|
|
|
|
|
|
|
|
//定义了一个静态的HashMap变量sContactCache和一个静态的字符串常量TAG。
|
|
|
|
private static HashMap<String, String> sContactCache;
|
|
|
|
private static HashMap<String, String> sContactCache;
|
|
|
|
private static final String TAG = "Contact";
|
|
|
|
private static final String TAG = "Contact";
|
|
|
|
|
|
|
|
//定义字符串常量CALLER_ID_SELECTION包含了整个查询的语句,其中使用了安卓提供的一些函数和常量,比如PHONE_NUMBERS_EQUAL函数和Phone.NUMBER常量。
|
|
|
|
|
|
|
|
//该查询语句作用是从小米设备的联系人数据库中设法获取和指定电话号码匹配的联系人的信息。
|
|
|
|
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
|
|
|
|
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
|
|
|
|
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
|
|
|
|
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
|
|
|
|
+ " AND " + Data.RAW_CONTACT_ID + " IN "
|
|
|
|
+ " AND " + Data.RAW_CONTACT_ID + " IN "
|
|
|
@ -36,6 +38,8 @@ public class Contact {
|
|
|
|
+ " FROM phone_lookup"
|
|
|
|
+ " FROM phone_lookup"
|
|
|
|
+ " WHERE min_match = '+')";
|
|
|
|
+ " WHERE min_match = '+')";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//定义了一个getContact用来接收context对象和电话号码作为参数,该方法先检查sContactCache中是否已经缓存了号码对应联系人信息,是的话直接返回联系人姓名。
|
|
|
|
|
|
|
|
//否则就构造一个查询语句,查询成功后将姓名缓存到sContactCache中,返回姓名;失败的话就返回null
|
|
|
|
public static String getContact(Context context, String phoneNumber) {
|
|
|
|
public static String getContact(Context context, String phoneNumber) {
|
|
|
|
if(sContactCache == null) {
|
|
|
|
if(sContactCache == null) {
|
|
|
|
sContactCache = new HashMap<String, String>();
|
|
|
|
sContactCache = new HashMap<String, String>();
|
|
|
@ -44,7 +48,8 @@ public class Contact {
|
|
|
|
if(sContactCache.containsKey(phoneNumber)) {
|
|
|
|
if(sContactCache.containsKey(phoneNumber)) {
|
|
|
|
return sContactCache.get(phoneNumber);
|
|
|
|
return sContactCache.get(phoneNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//将电话号码中的"+"替换为最小匹配的呼叫者ID,以生成新的查询条件。
|
|
|
|
|
|
|
|
//使用查询条件在联系人数据的URI(Data.CONTENT_URI)上进行查询,查询的结果只包含联系人的显示名称(Phone.DISPLAY_NAME)。
|
|
|
|
String selection = CALLER_ID_SELECTION.replace("+",
|
|
|
|
String selection = CALLER_ID_SELECTION.replace("+",
|
|
|
|
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
|
|
|
|
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
|
|
|
|
Cursor cursor = context.getContentResolver().query(
|
|
|
|
Cursor cursor = context.getContentResolver().query(
|
|
|
@ -54,6 +59,7 @@ public class Contact {
|
|
|
|
new String[] { phoneNumber },
|
|
|
|
new String[] { phoneNumber },
|
|
|
|
null);
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//如果查询结果不为空,将第一个结果的显示名称存储在缓存中,并返回该名称;否则,记录日志并返回null。
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String name = cursor.getString(0);
|
|
|
|
String name = cursor.getString(0);
|
|
|
|