main
pbhqclpae 7 months ago
parent a07c96aeb2
commit 03dbc2de38

@ -14,21 +14,22 @@
* limitations under the License.
*/
package net.micode.notes.data;
package net.micode.notes.data; //声明该类属于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 android.content.Context; //导入Context类用于获取应用的上下文
import android.database.Cursor; //导入Cursor类用于处理数据库查询的结果
import android.provider.ContactsContract.CommonDataKinds.Phone; //导入Phone类访问联系人相关信息
import android.provider.ContactsContract.Data; //导入Data类用于访问数据表
import android.telephony.PhoneNumberUtils; //导入PhoneNumberUtils类用于处理电话号码
import android.util.Log; //导入Log类用于输出日志
import java.util.HashMap;
import java.util.HashMap; //导入HashMap类用于存储电话号码和联系人名称的缓存
public class Contact {
private static HashMap<String, String> sContactCache;
private static final String TAG = "Contact";
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 "
@ -36,38 +37,47 @@ public class Contact {
+ " 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);
}
//使用PhoneNumberUtils将电话号码格式化为最小匹配格式
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
//通过ContentResolver执行查询操作查询联系人表中的名称
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME },
selection,
new String[] { phoneNumber },
null);
Data.CONTENT_URI, //查询Data内容提供者
new String [] { Phone.DISPLAY_NAME }, //查询显示名称字段
selection, //查询条件
new String[] { phoneNumber }, //查询参数,即电话号码
null); //排序条件为空
//如果查询结果不为空且有数据
if (cursor != null && cursor.moveToFirst()) {
try {
//获取联系人名称
String name = cursor.getString(0);
//将电话号码和联系人名称缓存到HashMap中
sContactCache.put(phoneNumber, name);
return name;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
} finally {
return name; //返回联系人名称
} catch (IndexOutOfBoundsException e) { //如果获取数据失败
Log.e(TAG, " Cursor get string error " + e.toString()); //输出错误日志
return null; //返回null表示未找到联系人
} finally { //确保查询完成后关闭游标
cursor.close();
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
} else { //如果没有找到联系人
Log.d(TAG, "No contact matched with number:" + phoneNumber); //输出日志表示没有找到匹配的联系人
return null; //返回null表示未找到联系人
}
}
}

Loading…
Cancel
Save