|
|
|
@ -24,7 +24,15 @@ import android.telephony.PhoneNumberUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @ProjectName: minode
|
|
|
|
|
* @Package: net.micode.notes.data
|
|
|
|
|
* @ClassName: Contact
|
|
|
|
|
* @Description: 该类实现建立联系人数据库的功能
|
|
|
|
|
* @Author: qijingxi
|
|
|
|
|
* @Date: 2024-12-30 15:13
|
|
|
|
|
*/
|
|
|
|
|
public class Contact {
|
|
|
|
|
private static HashMap<String, String> sContactCache;
|
|
|
|
|
private static final String TAG = "Contact";
|
|
|
|
@ -35,25 +43,32 @@ public class Contact {
|
|
|
|
|
+ "(SELECT raw_contact_id "
|
|
|
|
|
+ " FROM phone_lookup"
|
|
|
|
|
+ " WHERE min_match = '+')";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method getContact
|
|
|
|
|
* @description 该方法用于识别电话号码对应的联系人
|
|
|
|
|
* @date: 2024-12-30 15:13
|
|
|
|
|
* @author: qijingxi
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static String getContact(Context context, String phoneNumber) {
|
|
|
|
|
if(sContactCache == null) {
|
|
|
|
|
sContactCache = new HashMap<String, String>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找HashMap中是否有phoneNumber的信息
|
|
|
|
|
if(sContactCache.containsKey(phoneNumber)) {
|
|
|
|
|
return sContactCache.get(phoneNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String selection = CALLER_ID_SELECTION.replace("+",
|
|
|
|
|
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
|
|
|
|
|
Cursor cursor = context.getContentResolver().query(
|
|
|
|
|
// 查找数据库中phoneNumber的信息
|
|
|
|
|
Cursor cursor = context.getContentResolver().query(
|
|
|
|
|
Data.CONTENT_URI,
|
|
|
|
|
new String [] { Phone.DISPLAY_NAME },
|
|
|
|
|
selection,
|
|
|
|
|
new String[] { phoneNumber },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
// 检查是否查询到联系人,找到则将相关信息加入HashMap中,未找到则处理异常
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
String name = cursor.getString(0);
|
|
|
|
|