Android获取联系人信息

内容有点长.当初写的时候,边看源码,边对应着数据库一步步整理出来的....见代码

注:需传入上下文.context..为啥代码贴出来的那么不整齐............凌乱了.贴出的代码还嵌有html格式的...

获取联系人时.可能会有多种类型的电话号码、地址、EMAIL等(家里的,公司的,个人的等等..)

javabean属性,自己补全:

public class ContactPeople implements Serializable{


	private static final long serialVersionUID = 7004320659920318150L;
	/**
	 * 姓名
	 */
	private String contactName;
	/**
	 * 家庭电话
	 */
	private String contactHomePhone;
	/**
	 * 电话号码
	 */
	private String contactPhone;
	/**
	 * email
	 */
	private String contactEmail;
	/**
	 * 头像
	 */
	private byte[] contactPhoto;
	/**
	 * 地址
	 */
	private String contactAddress;
	/**
	 * 机构名
	 */
	private String contactOrganizCompany;
	/**
	 * 机构邮编
	 */
	private String contactOrganizPost;
	/**
	 * 备注
	 */
	private String contactRemark;
	/**
	 * 昵称
	 */
	private String contactNickname;
}

// 将所有联系人放入list
 List<ContactPeople> peoples = new ArrayList<ContactPeople>();

 Cursor cur = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null,null);//获取全部

 // 循环遍历
 if (cur.moveToFirst()) {
 int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
 int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
 do {
 // 联系人Bean 每读出一个联系人,就将其放入Bean中
 ContactPeople cp = new ContactPeople();
 // 获得联系人的ID号
 String contactId = cur.getString(idColumn);
 // 获得联系人姓名
 String disPlayName = cur.getString(displayNameColumn);
 // 查看该联系人有多少个电话号码。如果没有这返回值为0
 int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); cp.setContactName(disPlayName); if (phoneCount== 0) {
 // 获得联系人的电话号码
 Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
 if (phones.moveToFirst()) {
 do {
 // 遍历所有的电话号码
 String phoneNumber = phones.getString(phones
 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 String phoneType = phones.getString(phones
 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

 if (phoneType.equals("1")) {
 cp.setContactHomePhone(phoneNumber);
 } else {
 cp.setContactPhone(phoneNumber);
 }
 } while (phones.moveToNext());
 }
 }

 // 获取该联系人邮箱
 Cursor emails = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
 if (emails.moveToFirst()) {
 do {
 // 遍历所有的Email
 String emailValue = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
 strbuf.append("Email:" + emailValue + "\n");
 cp.setContactEmail(emailValue);
 } while (emails.moveToNext());
 } // 获得联系人头像
 Cursor image = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
 "DISPLAY_NAME = '" + disPlayName + "'", null, null);
 if (image.moveToFirst()) {
 do {
 // 遍历所有的头像
 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
 uri);
 byte[] contactPhoto = null;
 try {
 if (input != null) {
 contactPhoto = InputStreamToByte(input);
 }
 } catch (IOException e) {
 e.printStackTrace();
 }

 cp.setContactPhoto(contactPhoto);
 } while (image.moveToNext());
 }

 // 获取该联系人地址
 Cursor address = context.getContentResolver().query(
 ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
 if (address.moveToFirst()) {
 do {
 // 遍历所有的地址
 String street = address.getString(address
 .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
 String city = address.getString(address
 .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
 String region = address.getString(address
 .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
 String postCode = address.getString(address
 .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
 String formatAddress = address.getString(address
 .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));  cp.setContactAddress(formatAddress); } while (address.moveToNext());
 }


 // 获取该联系人组织
 Cursor organizations = context.getContentResolver().query(Data.CONTENT_URI,
 new String[] { Data._ID, Organization.COMPANY, Organization.TITLE },
 Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Organization.CONTENT_ITEM_TYPE + "'",
 new String[] { contactId }, null);
 if (organizations.moveToFirst()) {
 do {
 String company = organizations.getString(organizations.getColumnIndex(Organization.COMPANY));
 String post = organizations.getString(organizations.getColumnIndex(Organization.TITLE));  cp.setContactOrganizCompany(company); cp.setContactOrganizPost(post);
 } while (organizations.moveToNext());
 }


 // 获取备注信息
 Cursor notes = context.getContentResolver().query(Data.CONTENT_URI, new String[] { Data._ID, Note.NOTE },
 Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Note.CONTENT_ITEM_TYPE + "'",
 new String[] { contactId }, null);
 if (notes.moveToFirst()) {
 do {
 String noteinfo = notes.getString(notes.getColumnIndex(Note.NOTE));  cp.setContactRemark(noteinfo); } while (notes.moveToNext());
 }


 // 获取nickname信息
 Cursor nicknames = context.getContentResolver().query(Data.CONTENT_URI, new String[] { Data._ID, Nickname.NAME },
 Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Nickname.CONTENT_ITEM_TYPE + "'",
 new String[] { contactId }, null);
 if (nicknames.moveToFirst()) {
 do {
 String nickname_ = nicknames.getString(nicknames.getColumnIndex(Nickname.NAME));
//						Log.i("nickname_", nickname_);
 cp.setContactNickname(nickname_);
 } while (nicknames.moveToNext());
 }
 peoples.add(cp);
 } while (cur.moveToNext());

 }

转载于:https://my.oschina.net/yyplayer/blog/130182

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值