From 5cc0f68682a70462cc9bcba0b1ec9389233cd38a Mon Sep 17 00:00:00 2001 From: dingxinnan <1175943266@qq.com> Date: Thu, 21 Dec 2023 00:30:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/micode/notes/data/Contact.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Notes-master1/app/src/main/java/net/micode/notes/data/Contact.java b/src/Notes-master1/app/src/main/java/net/micode/notes/data/Contact.java index d97ac5d..ea6f137 100644 --- a/src/Notes-master1/app/src/main/java/net/micode/notes/data/Contact.java +++ b/src/Notes-master1/app/src/main/java/net/micode/notes/data/Contact.java @@ -26,9 +26,12 @@ import android.util.Log; import java.util.HashMap; public class Contact { + // 定义一个HashMap类型的静态变量sContactCache private static HashMap 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(); } + // 判断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; } } -} +} \ No newline at end of file