/* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* *文件名:Contact.java * 修改人:蒙程 * 修改时间:2025-12-13 * 修改内容:增加代码注释,标注的代码行数共有21行 */ package net.micode.notes.data; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.Data; import android.telephony.PhoneNumberUtils; import android.util.Log; import java.util.HashMap; /** *Contact类是联系人工具类 * *
根据“提供的电话号码反向查询联系人姓名”的能力,并带内存缓存,避免重复查库。
* 本类所有方法均为静态,无需实例化。 * * @author 蒙程 * @version 1.0 * @since [起始版本] */ public class Contact { //HashMap表示联系人和电话号码之间的映射关系。只声明,不new,是延迟初始化的表现 private static HashMap优先读取内存缓存,未命中时查询系统联系人数据库,并将结果写入缓存。
* @param context 上下文,用于访问ContentResolver * @param phoneNumber 待查询的电话号码(可为国际格式、带前缀 0 等) * @return 联系人姓名;未匹配到或者发生异常时,返回@code null * @throws * @see android.provider.ContactsContract.CommonDataKinds */ public static String getContact(Context context, String phoneNumber) { //不到真正要用的时候,不给对象分配内存。第一次被调用时才new。 if(sContactCache == null) { sContactCache = new HashMap