android 2.2 通讯录
//1. 查询
/**
* 根据联系人ID获取该联系人的详细信息
*/
public ContactorInfo getContactorInfoById(ContentResolver resolver,
int contactId) {
// 根据联系人ID查contacts表
Cursor cursor = resolver.query(Contacts.CONTENT_URI, null, Contacts._ID
+ "=" + contactId, null, null);
ContactorInfo info = null;
if (cursor.moveToFirst()) {
// 得到姓名
String contactName = cursor.getString(cursor
.getColumnIndex(Phone.DISPLAY_NAME));
// 得到电话
String phoneNumber = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
// 得到联系人头像ID
Long photoid = cursor.getLong(cursor
.getColumnIndex(Contacts.PHOTO_ID));
// 得到联系人头像Bitamp
Bitmap contactPhoto = null;
// photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
} else {
contactPhoto = BitmapFactory.decodeResource(
activity.getResources(), DEFAULT_PHOTO_ID);
}
// 获取联系人email
Cursor emails = resolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "="
+ contactId, null, null);
List<String> emaillist = new ArrayList<String>();
while (emails.moveToNext()) {
String email = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emaillist.add(email);
}
emails.close();
info = new ContactorInfo();
info.setContactorName(contactName);
info.setContactorNum(phoneNumber);
info.setContactorPhoto(contactPhoto);
info.setContactorId(String.valueOf(contactId));
info.setEmails(emaillist);
System.out.println("contactorinfo-->" + contactName + "\n"
+ phoneNumber + "\n" + contactPhoto + "\n" + contactId
+ "\n" + emaillist);
}
cursor.close();
return info;
}
// 分组信息
1 /**
2 * 根据联系人ID获取联系人的分组信息
3 */
4 private static HashMap<String, String> getContactorGroupInfo(
5 ContentResolver resolver, int contactId) {
6 HashMap<String, String> resultMap = new HashMap<String, String>();
7 // 根据联系人ID查询Data表,获得该联系人在data表中的分组信息
8 final Cursor dataCursor = resolver.query(Data.CONTENT_URI, null,
9 Data.RAW_CONTACT_ID + " = " + contactId + " AND "
10 + Data.MIMETYPE + "='"
11 + GroupMembership.CONTENT_ITEM_TYPE + "'", null, null);
12 String groupName = DEFAULT_GROUP_NAME;
13 int groupId = -1;
14 if (dataCursor.moveToFirst()) {
15 do {
16 // 取得分组的组ID
17 groupId = dataCursor.getInt(dataCursor
18 .getColumnIndex(Data.DATA1));
19 // 查询Group表,获得groupId对应的组名
20 Cursor groupCursor = resolver.query(Groups.CONTENT_URI,
21 new String[] { Groups.TITLE }, Groups._ID + "="
22 + groupId, null, null);
23 if (groupCursor.moveToFirst()) {
24 groupName = groupCursor.getString(groupCursor
25 .getColumnIndex(Groups.TITLE));
26 }
27 groupCursor.close();
28
29 } while (dataCursor.moveToNext());
30 }
31 dataCursor.close();
32
33 resultMap.put("groupId", String.valueOf(groupId));
34 resultMap.put("groupName", groupName);
35
36 return resultMap;
37 }
URI:
ContactsContract.Contacts.CONTENT_URI (content://com.android.contacts/contacts)
ContactsContract.Contacts._ID
ContactsContract.Contacts.DISPLAY_NAME
ContactsContract.Contacts.HAS_PHONE_NUMBER
ContactsContract.CommonDataKinds.Phone.CONTENT_URI (content://com.android.contacts/data/phones)
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
ContactsContract.CommonDataKinds.Phone.NUMBER
ContactsContract.CommonDataKinds.Email.CONTENT_URI (content://com.android.contacts/data/emails)
ContactsContract.CommonDataKinds.Email.CONTACT_ID
ContactsContract.CommonDataKinds.Email.DATA
ContactsContract.CommonDataKinds.Email.TYPE
ContactsContract.Data.CONTENT_URI (content://com.android.contacts/data)(ContactsContract.Data.CONTENT_TYPE = "vnd.android.cursor.dir/data")
Note:
ContactsContract.Data.MIMETYPE-->ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE
ContactsContract.Data.DATA1-->ContactsContract.CommonDataKinds.Note.NOTE
Postal:
ContactsContract.Data.MIMETYPE-->ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
ContactsContract.Data.DATA1-->ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
ContactsContract.Data.DATA5-->ContactsContract.CommonDataKinds.StructuredPostal.POBOX
ContactsContract.Data.DATA4-->ContactsContract.CommonDataKinds.StructuredPostal.STREET
ContactsContract.Data.DATA7-->ContactsContract.CommonDataKinds.StructuredPostal.CITY
ContactsContract.Data.DATA9-->ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE
Email:
ContactsContract.Data.MIMETYPE-->ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ("vnd.android.cursor.item/email_v2")
ContactsContract.Data.DATA1-->ContactsContract.CommonDataKinds.Email.ADDRESS
ContactsContract.Data.DATA4-->ContactsContract.CommonDataKinds.Email.DISPLAY_NAME
Phone:
ContactsContract.Data.MIMETYPE-->ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE ("vnd.android.cursor.item/phone_v2")
ContactsContract.Data.DATA1-->ContactsContract.CommonDataKinds.Phone.NUMBER
Photo:ContactsContract.Data.MIMETYPE-->ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE ("vnd.android.cursor.item/photo")
ContactsContract.Data.DATA15-->ContactsContract.CommonDataKinds.Photo.PHOTO
Im:ContactsContract.Data.MIMETYPE-->ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE ("vnd.android.cursor.item/im")
ContactsContract.Data.DATA5-->ContactsContract.CommonDataKinds.Im.PROTOCOL
ContactsContract.Data.DATA6-->ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL
ContactsContract.Data.DATA1-->ContactsContract.CommonDataKinds.Im.DATA (从CommonColumns继承而来)
ContactsContract.Data.DATA2-->ContactsContract.CommonDataKinds.Im.TYPE (从CommonColumns继承而来)
备注
自定义备注可以被加入到联系人数据中,ContactsContract.CommonDataKinds.Email.CONTACT_ID这些被存储在独立的表中,但与联系人的ID建立了外键关联。
备注被存储在Android联系人的通用data表中。当查询特定的数据时,WHERE从句需要两个条件从句。首先是标准ID,然后是MIMETYPE 。Android SDK包含了一系列自动生成的变量来维护这个字段。用ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE来限定备注记录。data表URI在ContactsContract.Data.CONTENT_UR中。最后备注的字段名保存在ContactsContract.CommonDataKinds.Note.NOTE
String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] noteWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
}
noteCur.close();
邮政地址
Android可以为联系人存储多个邮政地址,地址跟备注一样也是被保存在data表中,URI是ContactsContract.Data.CONTENT_URI与备注类似。我们需要一个MIMETYPE 作为WHERE条件从句。在2.0中,地址数据被分为多个区域,包含了地址的不同部分:(邮箱,街道,城市,地区,邮政编码)在Android SDK的早期版本中,这只是一个不规则的字符。
该字段名称都保存在 ContactsContract.CommonDataKinds.StructuredPostal
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, where, whereParameters, null);
while(addrCur.moveToNext()) {
String poBox = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
}
addrCur.close();
即时消息
即时消息的查询就像备注和地址。IM的相关数据里的主要字段名称保存在 ContactsContract.CommonDataKinds.Im
String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] imWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(
imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
String imType;
imType = imCur.getString(
imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
}
imCur.close();
组织机构
最后要讲述的联系人记录是机构数据。Android 联系人记录包含的信息有雇主,专业,社会关系,以及角色与职位,这些资料记录都保存在ContactsContract.Data.CONTENT_URI主要的机构数据里的主要字段名称则保存在ContactsContract.CommonDataKinds.Organization
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
}
orgCur.close();