ContactUtils.java
package com.chen.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; /** * 联系人操作工具类,提供姓名,手机,邮箱的增删改查操作 * 如果某一项数据 例如,同一联系人存在多条手机号 , 此类此类只能显示和操作其中的第一条数据 * 其中操作格式为id,name,phone,email; * * @author chenj_000 * */ public class ContactUtils { /** * raw_contacts表 */ public static Uri RAW_CONTACTS = Uri .parse("content://com.android.contacts/raw_contacts"); /** * data表 */ public static Uri DATA = Uri.parse("content://com.android.contacts/data"); public static final int TYPE_PHONE = 5, TYPE_NAME = 7, TYPE_EMAIL = 1; public static final String[] MIMETYPE = { "", "vnd.android.cursor.item/email_v2", "vnd.android.cursor.item/im", "vnd.android.cursor.item/nickname", "vnd.android.cursor.item/organization", "vnd.android.cursor.item/phone_v2", "vnd.android.cursor.item/sip_address", "vnd.android.cursor.item/name", "vnd.android.cursor.item/postal-address_v2", "vnd.android.cursor.item/identity", "vnd.android.cursor.item/photo", "vnd.android.cursor.item/group_membership" }; private static final String CONDITION = "raw_contact_id=? and mimetype_id=?"; /** * 查询所有联系人信息 * * @return 以list形式返回存放联系人信息的map集合,其中包括的键有id,name,phone,email */ public static List<Map<String, String>> query(ContentResolver resolver) { List<Map<String, String>> datas = new ArrayList<Map<String, String>>(); Cursor cursor = resolver.query(RAW_CONTACTS, null, null, null, null); HashMap<String, String> map; while (cursor.moveToNext()) { map = new HashMap<String, String>(); map.put("id", getString(cursor, "_id")); map.put("name", getString(cursor, "display_name")); map.put("phone", getString(getCursor(map.get("id"), TYPE_PHONE, resolver), "data1")); map.put("email", getString(getCursor(map.get("id"), TYPE_EMAIL, resolver), "data1")); datas.add(map); } return datas; } /** * 从当前Cursor中找出列名为columnName的字段 * * @param c * 数据源 * @param columnName * 列名 * @return 列中的数据,数据源为空时返回结果为null */ private static String getString(Cursor c, String columnName) { if (c == null) { return null; } return c.getString(c.getColumnIndex(columnName)); } /** * 删除联系人 * * @param resolver * @param id * @return 删除是否成功 */ public static boolean delete(ContentResolver resolver, String id) { if (resolver.delete(RAW_CONTACTS, "_id=" + id, null) > 0) { return true; } return false; } /** * 更新联系人信息,联系人不存在时返回null * * @param resolver * @param id * raw_contact_id * @param name * @param phone * @param email */ public static boolean update(ContentResolver resolver, String id, String name, String phone, String email) { ContentValues values = new ContentValues(); // 更新raw_contact表中的姓名 values = new ContentValues(); values.put("display_name", name); if (resolver.update(RAW_CONTACTS, values, "_id=" + id, null) == 0) // 当前不存在此数据 return false; /* * 更新data表中的姓名 */ resolver.update(DATA, getDataValues(name, name), CONDITION, new String[] { id, "" + TYPE_NAME }); /* * 更新data表手机号码 */ if (resolver.update(DATA, getDataValues(phone, "2"), CONDITION, new String[] { id, "" + TYPE_PHONE }) == 0) { // 当前联系人号码为空,插入号码 resolver.insert(DATA, getDataValues(id, MIMETYPE[TYPE_PHONE], phone)); } /* * 更新data表邮箱 */ if (resolver.update(DATA, getDataValues(email, "1"), CONDITION, new String[] { id, "" + TYPE_EMAIL }) == 0) { // 当前邮箱不存在,插入邮箱 resolver.insert(DATA, getDataValues(id, MIMETYPE[TYPE_EMAIL], email)); } return true; } /** * 用于更新数据时包装数据对象 * * @param data1 * @param data2 * @return */ private static ContentValues getDataValues(String data1, String data2) { ContentValues values = new ContentValues(); values.put("data1", data1); values.put("data2", data2); return values; } /** * 插入联系人信息 * * @param resolver * @param name * @param phone * @param email */ public static void insert(ContentResolver resolver, String name, String phone, String email) { // 向raw_contact表中插入一条空数据,并获得一个id String id = "" + ContentUris.parseId(resolver.insert(RAW_CONTACTS, new ContentValues())); // 向data表插入邮箱 resolver.insert(DATA, getDataValues(id, MIMETYPE[TYPE_EMAIL], email)); // 向data表插入手机号码 resolver.insert(DATA, getDataValues(id, MIMETYPE[TYPE_PHONE], phone)); // 向data表插入姓名 resolver.insert(DATA, getDataValues(id, MIMETYPE[TYPE_NAME], name)); } /** * 将数据包装为ContentValues对象 * * @param id * raw_contact_id * @param mimetype * @param data1 * @return */ private static ContentValues getDataValues(String id, String mimetype, String data1) { ContentValues values = new ContentValues(); values.put("raw_contact_id", id); values.put("mimetype", mimetype); values.put("data1", data1); return values; } /** * 在data表中查询符合raw_contact_id,mimetype_id的第一条数据,数据不存在时返回null * * @param raw_contact_id * @param mimetype_id * @return */ private static Cursor getCursor(String raw_contact_id, int mimetype_id, ContentResolver resolver) { Cursor c = resolver.query(DATA, null, CONDITION, new String[] { raw_contact_id, "" + mimetype_id }, null); if (c.moveToNext()) return c; return null; } }