Update Contact.java

main
pf45q8f3g 7 months ago
parent 8128616af3
commit 8d8167ca08

@ -1,17 +1,15 @@
/* /*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * (c) 2010-2011, MiCode (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * 使 Apache License, Version 2.0
* 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 * 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;
@ -25,22 +23,24 @@ import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
/**
import android.content.Context; *
import android.content.ContentResolver; * 使
import android.database.Cursor; */
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.util.Log;
import android.telephony.PhoneNumberUtils;
import java.util.HashMap;
public class Contact { public class Contact {
/**
*
*/
private static HashMap<String, String> sContactCache; private static HashMap<String, String> sContactCache;
/**
*
*/
private static final String TAG = "Contact"; private static final String TAG = "Contact";
// 用于查询联系人的SQL语句 /**
* SQL
*
*/
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN " + " AND " + Data.RAW_CONTACT_ID + " IN "
@ -49,28 +49,28 @@ public class Contact {
+ " WHERE min_match = '+')"; + " WHERE min_match = '+')";
/** /**
* *
* *
* @param context * @param context
* @param phoneNumber * @param phoneNumber
* @return null * @return null
*/ */
public static String getContact(Context context, String phoneNumber) { public static String getContact(Context context, String phoneNumber) {
// 初始化联系人缓存 // 初始化缓存
if (sContactCache == null) { if (sContactCache == null) {
sContactCache = new HashMap<String, String>(); sContactCache = new HashMap<String, String>();
} }
// 检查缓存中是否已有该电话号码对应的联系人姓名 // 首先检查缓存中是否存在该电话号码对应的联系人姓名
if (sContactCache.containsKey(phoneNumber)) { if (sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber); return sContactCache.get(phoneNumber);
} }
// 替换SQL语句中的占位符为实际的电话号码匹配模式 // 将 SQL 语句中的占位符替换为实际的电话号码匹配模式
String selection = CALLER_ID_SELECTION.replace("+", String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 查询联系人数据库 // 执行数据库查询操作
Cursor cursor = context.getContentResolver().query( Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI, Data.CONTENT_URI,
new String[]{Phone.DISPLAY_NAME}, new String[]{Phone.DISPLAY_NAME},
@ -81,46 +81,23 @@ public class Contact {
// 处理查询结果 // 处理查询结果
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
try { try {
// 获取联系人姓名 // 获取联系人姓名并缓存
String name = cursor.getString(0); String name = cursor.getString(0);
// 将结果缓存起来
sContactCache.put(phoneNumber, name); sContactCache.put(phoneNumber, name);
return name; return name;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
// 日志记录错误 // 记录异常信息
Log.e(TAG, "Cursor get string error " + e.toString()); Log.e(TAG, "游标获取字符串时出错:" + e.toString());
return null; return null;
} finally { } finally {
// 关闭游标 // 确保游标关闭,释放资源
cursor.close(); cursor.close();
} }
} else { } else {
// 日志记录未找到匹配联系人 // 记录未找到匹配联系人的信息
Log.d(TAG, "No contact matched with number:" + phoneNumber); Log.d(TAG, "未找到与号码匹配的联系人:" + phoneNumber);
return null; return null;
} }
} }
} }
/**
*
Contact 使 sContactCache
sContactCache
TAG
CALLER_ID_SELECTIONSQLMIME
getContact
context
phoneNumber
null
SQL
null
使try-catchIndexOutOfBoundsException
使finally使
*/
Loading…
Cancel
Save