From 6e103814ea31fd2237d6887683eb30e704aca930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=80=B8=E9=A3=9E?= <1551759347@qq.com> Date: Sun, 9 Apr 2023 22:34:11 +0800 Subject: [PATCH] 1 --- .../java/net/micode/notes/data/Contact.java | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/Notes-master3/app/src/main/java/net/micode/notes/data/Contact.java b/src/Notes-master3/app/src/main/java/net/micode/notes/data/Contact.java index d97ac5d..68a4e6a 100644 --- a/src/Notes-master3/app/src/main/java/net/micode/notes/data/Contact.java +++ b/src/Notes-master3/app/src/main/java/net/micode/notes/data/Contact.java @@ -15,8 +15,14 @@ */ package net.micode.notes.data; +/** + * 当前文件所在的包名 + */ import android.content.Context; +/** + * Context类是Android自带的类不在包里,就要用import android.content.Context;将它导入,就相当于自己写的类。后面的同理 + */ import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.Data; @@ -25,7 +31,14 @@ import android.util.Log; import java.util.HashMap; -public class Contact { +/** + * 引用了STL里面的HashMap,用来映射联系人和电话号码 + */ +public class Contact + /** + * 通过两种方式访问联系人1.去数据库中2.去cache中,如果数据库中读了,将它存入cache中,和主存缓存机制类似 + */ +{ private static HashMap sContactCache; private static final String TAG = "Contact"; @@ -36,34 +49,42 @@ public class Contact { + " FROM phone_lookup" + " WHERE min_match = '+')"; - public static String getContact(Context context, String phoneNumber) { - if(sContactCache == null) { + /** + * 定义字符串CALLER_ID_SELECTION,用于匹配查找联系人 + * phone number + * data.mimetype 数据类型 + * item.type 联系人类型 + * 联系人ID + */ + + public static String getContact(Context context, String phoneNumber) /**获取联系人**/{ + if(sContactCache == null) /**联系人的cache为空,就初始化一个**/{ sContactCache = new HashMap(); } - if(sContactCache.containsKey(phoneNumber)) { + if(sContactCache.containsKey(phoneNumber)) /**查找cache中是否有phoneNumber对应的联系人信息**/{ return sContactCache.get(phoneNumber); } String selection = CALLER_ID_SELECTION.replace("+", - PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); + PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));/**toCallerIDMinMatch是安卓自带的号码匹配工具,截取查询号码的后7位作为匹配依据**/ Cursor cursor = context.getContentResolver().query( Data.CONTENT_URI, - new String [] { Phone.DISPLAY_NAME }, - selection, + new String [] { Phone.DISPLAY_NAME },/**这个参数告诉查询要返回的列(Column),Contacts Provider提供了联系人的ID和联系人的NAME等内容,在这里,我们只需要NAME,所以提供这个参数DISPLAY_NAME**/ + selection,/**selection,设置条件,相当于SQL语句中的where**/ new String[] { phoneNumber }, null); - if (cursor != null && cursor.moveToFirst()) { - try { + if (cursor != null && cursor.moveToFirst()) {/**若数据库存在,则游标移动到游标所表示的信息元组的第一行开始**/ + try {/**联系人姓名,并将相关信息存入cache中**/ String name = cursor.getString(0); sContactCache.put(phoneNumber, name); return name; - } catch (IndexOutOfBoundsException e) { + } catch (IndexOutOfBoundsException e) {/**参数越界异常**/ Log.e(TAG, " Cursor get string error " + e.toString()); - return null; + return null;/**引入的util.log就是java自带的日志输出工具,而log.e则代表error,显示的颜色为红色**/ } finally { - cursor.close(); + cursor.close();/**关闭cursor游标**/ } } else { Log.d(TAG, "No contact matched with number:" + phoneNumber);