From 47aa745785404a9f94fefe9f9eccd66ed7c8d2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E6=B5=A9?= <1260788704@qq.com> Date: Mon, 11 Dec 2023 23:52:21 +0800 Subject: [PATCH] remake src --- src/data/Contact.java | 73 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/data/Contact.java diff --git a/src/data/Contact.java b/src/data/Contact.java new file mode 100644 index 0000000..0a4cbe1 --- /dev/null +++ b/src/data/Contact.java @@ -0,0 +1,73 @@ +/* + * 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. + */ + +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; + +public class Contact { + private static HashMap sContactCache; //用散列表缓存联系人 + private static final String TAG = "Contact"; //标识设为”Contact“ + + private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUHashMapMBER + + ",?) 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(); + } //若缓存区为空,创建新的散列表 + + if(sContactCache.containsKey(phoneNumber)) { + return sContactCache.get(phoneNumber); + } //查找并返回一个电话号码在联系人缓存中对应的值 + + String selection = CALLER_ID_SELECTION.replace("+", + PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将传入的电话号码转换为一个可用于最小匹配的调用者ID格式 + Cursor cursor = context.getContentResolver().query( + Data.CONTENT_URI, + new String [] { Phone.DISPLAY_NAME }, + selection, + new String[] { phoneNumber }, + null);//从联系人数据库中查询与phoneNumber匹配的联系人的显示名称。查询结果将被存储在返回的Cursor对象中,可以遍历这个Cursor来获取每个匹配联系人的显示名称。 + + if (cursor != null && cursor.moveToFirst()) { + try { + String name = cursor.getString(0); + sContactCache.put(phoneNumber, name); + return name; + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, " Cursor get string error " + e.toString()); + return null; + } finally { + cursor.close(); + } + } else { + Log.d(TAG, "No contact matched with number:" + phoneNumber); + return null;//从Android设备的联系人数据库中检索与特定电话号码匹配的联系人名称 + } + } +}