pull/23/head
谢成扬 3 years ago
parent 944fc6a29a
commit 78bc543d11

@ -15,8 +15,14 @@
*/
package net.micode.notes.data;
/**
*
*/
import android.content.Context;
/**
* ContextAndroidimport android.content.Context;
*/
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
@ -25,7 +31,14 @@ import android.util.Log;
import java.util.HashMap;
public class Contact {
/**
* STLHashMap
*/
public class Contact
/**
* 访1.2.cachecache
*/
{
private static HashMap<String, String> sContactCache;
private static final String TAG = "Contact";
@ -36,34 +49,42 @@ public class Contact {
+ " FROM phone_lookup"
+ " WHERE min_match = '+')";
public static String getContact(Context context, String phoneNumber) {
if(sContactCache == null) {
/**
* CALLER_ID_SELECTION
* phone number
* data.mimetype
* item.type
* ID
*/
public static String getContact(Context context, String phoneNumber) /**获取联系人**/{
if(sContactCache == null) /**联系人的cache为空就初始化一个**/{
sContactCache = new HashMap<String, String>();
}
if(sContactCache.containsKey(phoneNumber)) {
if(sContactCache.containsKey(phoneNumber)) /**查找cache中是否有phoneNumber对应的联系人信息**/{
return sContactCache.get(phoneNumber);
}
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));/**toCallerIDMinMatch是安卓自带的号码匹配工具截取查询号码的后7位作为匹配依据**/
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME },
selection,
new String [] { Phone.DISPLAY_NAME },/**这个参数告诉查询要返回的列ColumnContacts Provider提供了联系人的ID和联系人的NAME等内容在这里我们只需要NAME所以提供这个参数DISPLAY_NAME**/
selection,/**selection设置条件相当于SQL语句中的where**/
new String[] { phoneNumber },
null);
if (cursor != null && cursor.moveToFirst()) {
try {
if (cursor != null && cursor.moveToFirst()) {/**若数据库存在,则游标移动到游标所表示的信息元组的第一行开始**/
try {/**联系人姓名并将相关信息存入cache中**/
String name = cursor.getString(0);
sContactCache.put(phoneNumber, name);
return name;
} catch (IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {/**参数越界异常**/
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
return null;/**引入的util.log就是java自带的日志输出工具而log.e则代表error显示的颜色为红色**/
} finally {
cursor.close();
cursor.close();/**关闭cursor游标**/
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber);

Loading…
Cancel
Save