维护打电话功能

liming
laimaxgg 11 months ago
parent ee1263ab6c
commit eeccfcde1b

@ -1,74 +1,79 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* c2010-2011The MiCodewww.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
* Apache2.0
* 使
*
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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; // 定义包名
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 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; // 导入HashMap类用于存储联系人缓存
import java.util.HashMap;
public class Contact { // 定义Contact类
private static HashMap<String, String> sContactCache; //静态哈希表,用于缓存联系人信息
private static final String TAG = "Contact"; // 日志标签
public class Contact {
private static HashMap<String, String> sContactCache;
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 "
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
+ " WHERE min_match = '+')"; // 查询条件,用于匹配来电号码
+ " WHERE min_match = '+')";
public static String getContact(Context context, String phoneNumber) { // 获取联系人信息的方法
if(sContactCache == null) { // 检查缓存是否为空
sContactCache = new HashMap<String, String>(); // 初始化联系人缓存
/**
*
*
* @param context
* @param phoneNumber
* @return null
*/
public static String getContact(Context context, String phoneNumber) {
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
if(sContactCache.containsKey(phoneNumber)) { // 如果缓存中存在该电话号码
return sContactCache.get(phoneNumber); // 直接返回缓存中的联系人名称
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); // 替换查询条件中的占位符
Cursor cursor = context.getContentResolver().query( // 执行查询
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME }, // 查询返回的字段为联系人显示名称
selection, // 查询条件
new String[] { phoneNumber }, // 查询参数
null); // 排序方式
new String [] { Phone.DISPLAY_NAME },
selection,
new String[] { phoneNumber },
null);
if (cursor != null && cursor.moveToFirst()) { // 如果查询结果不为空且移动到第一条记录
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; // 返回空
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(); // 关闭游标
cursor.close();
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber); // 记录未找到联系人的日志
return null; // 返回空
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
}
}
}
}

Loading…
Cancel
Save