You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MiNotes/other/06_210340012金宏武_代码标注/Contact.java

99 lines
4.9 KiB

2 years ago
/*
* 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;
2 years ago
//导入android自带的Context类
import android.content.Context;
2 years ago
//从数据库导入cursor表示数据库中的每行数据下标
import android.database.Cursor;
2 years ago
//android系统应用开发框架功能之一提供访问android内容提供者的类。导入里面联系人的号码这个是需要CommonDataKinds提供权限
import android.provider.ContactsContract.CommonDataKinds.Phone;
2 years ago
//导入Data包含通讯录中的每个人的全部信息。
import android.provider.ContactsContract.Data;
2 years ago
//1.Telephony 服务为电话应用程序编程接口 (TAPI) 提供支持。通过使用此硬件您可以通过直接连接到本地计算机、电话线、LAN、WAN 和 Internet 以进行通信。提供处理Phone Number的各种应用工具
import android.telephony.PhoneNumberUtils;
2 years ago
//安卓的日志类工具,方便后面调试使用
import android.util.Log;
2 years ago
//引用了STL里面的HashMap用来映射联系人和电话号码
import java.util.HashMap;
2 years ago
//change
2 years ago
public class Contact { //联系人类
private static HashMap<String, String> sContactCache;
2 years ago
//写入Cache而创建的一个成员变量
private static final String TAG = "Contact";
2 years ago
//在日志中查看由哪个类打印的日志
// 定义字符串CALLER_ID_SELECTION表示为每个用户存储了相应的信息当插入新的用户时系统会检查系统中是否已存在用户
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL("
2 years ago
+ 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 = '+')";
// 获取联系人
2 years ago
/**
*
*
* @param context
* @param phoneNumber
* @return
*/
public static String getContact(Context context, String phoneNumber) {
2 years ago
//判断联系人cache是否为空如果为空则初始化一个cache
if (sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
2 years ago
// 查找HashMap中是否已有对应联系人phoneNumber信息
if (sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
2 years ago
// 修改原先数据库语句Caller_ID_SELECTION用函数形参phonenumber的后七位电话号码代替"+"
String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
//从数据库中读取数据query是查询方法。cursor类是对数据库的操作获取整行的数据.Ressolver是对联系人内容的读取其中provider提供了联系人数据
Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI,//uri是区别provider的标志
new String[]{Phone.DISPLAY_NAME},
//该参数告诉provider要返回的内容这里是要返回联系人的名字
selection,
2 years ago
//该参数相当于数据库中where条件筛选只拥有特定电话号码的信息
new String[]{phoneNumber},
//筛选条件即参数phonenumber
null);
2 years ago
//设置查询是否按顺序排列,这里的意思是不按顺序排列
/**
*
*
*/
if (cursor != null && cursor.moveToFirst()) {
try {
2 years ago
// 获取联系人姓名,写入联系人缓存
String name = cursor.getString(0);
sContactCache.put(phoneNumber, name);
return name;
// 异常
} catch (IndexOutOfBoundsException e) {
2 years ago
// 将异常信息写入日志
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
} finally {
cursor.close();
}
2 years ago
// 未找到相关信息就写入日志
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
}
}
}