pull/1/head
git1 5 months ago
parent 8cde369c9e
commit ad0e8103d9

@ -0,0 +1,93 @@
/*
* 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;//导入Context类用于访问应用环境
import android.database.Cursor;// 导入 Cursor 类,用于操作查询结果
import android.provider.ContactsContract.CommonDataKinds.Phone;// 导入电话常量
import android.provider.ContactsContract.Data;// 导入数据常量
import android.telephony.PhoneNumberUtils;// 导入电话工具类
import android.util.Log; // 导入日志工具类
import java.util.HashMap;// 导入 HashMap 类,用于存储联系人缓存
public class Contact { // 定义 Contact 类
private static HashMap<String, String> sContactCache;
// 静态 HashMap 用于缓存联系人姓名
private static final String TAG = "Contact";
// 日志标签,用于标识日志信息
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 "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
+ " WHERE min_match = '+')"; // 查询选项字符串,用于匹配来电显示的电话号码
//根据电话号码获取联系人姓名的方法
public static String getContact(Context context, String phoneNumber) {
//初始化联系人缓存
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
//如果缓存中已存在该电话号码,则直接返回对应的联系人姓名
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
//替换查询字符串中的“+”,使用电话号码的最小匹配形式
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
//执行内容解析器查询,获取与电话号码匹配的联系人姓名
Cursor cursor = context.getContentResolver().query(
//查询的 UIR
Data.CONTENT_URI,
//查询的列,这里只查询显示姓名
new String [] { Phone.DISPLAY_NAME },
//查询条件
selection,
//查询参数
new String[] { phoneNumber },
//排序方式
null);
//检查游标是否有效,并尝试移动到第一条记录
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());
// 返回 null
return null;
} finally {
// 确保游标被关闭以释放资源
cursor.close();
}
} else { // 如果没有找到匹配的联系人
// 记录调试日志
Log.d(TAG, "No contact matched with number:" + phoneNumber);
// 返回 null
return null;
}
}
}
Loading…
Cancel
Save