读取联系人信息,包括ID、name、number、email、address

/**
	 * 此方法从数据库获取联系人信息,包装成对象,放到list集合里
	 * @return 包含所有联系人对象的集合
	 */
	public List<Contact> getContacts() {
		List<Contact> contactList = new ArrayList<Contact>();
		ContentResolver resolver = context.getContentResolver();
		Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while (cursor.moveToNext()) {
			Contact contact = new Contact();
			//联系人id
			String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
			contact.setContactId(contactId);
			Log.i(TAG, "…contactId…" + contactId);
			//联系人姓名
			String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
			Log.i(TAG, "…name…" + name);
			contact.setName(name);
			//判断联系人是否有联系电话
			String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
			if (hasPhone.equalsIgnoreCase("1")) {
				hasPhone = "true";
			} else {
				hasPhone = "false";
			}
			//查询sortKey,如对应张三的sortKey内容为张zhang三san,可以用来实现排序和按首字母查找
			String sortKey = cursor.getString(cursor.getColumnIndex(SORT_KEY));
			if (!TextUtils.isEmpty(sortKey)) {
				contact.setSortKey(sortKey);
			} else {
				contact.setSortKey("");
			}

			//如果有电话,根据联系人的ID查找到联系人的电话,电话可以是多个
			if (Boolean.parseBoolean(hasPhone)) {
				Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
				while (phones.moveToNext()) {
					String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					if (TextUtils.isEmpty(number)) {
						contact.setPhoneNum("");
						contact.setTelephonNum("");
					} else {
						if (number.startsWith("1")) {
							contact.setPhoneNum(number);
						} else if (number.startsWith("0")) {
							contact.setTelephonNum(number);
						}
					}
					Log.i(TAG, "…phoneNumber…  " + number);
				}
				phones.close();
			}

			//查找email地址,这里email也可以有多个
			Cursor emails = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
			while (emails.moveToNext()) {
				String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
				if (TextUtils.isEmpty(email)) {
					contact.setEmail("");
				} else {
					contact.setEmail(email);
				}
				Log.i(TAG, "…emailAddress…  " + email);
			}
			emails.close();

			// 获取该联系人地址  
			Cursor addrCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
			if (addrCursor.moveToFirst()) {
				String address = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
				contact.setAddress(address);
			}
			addrCursor.close();

			contactList.add(contact);
		}
		return contactList;
	}

Contact类

public class Contact {
	private String contactId;
	private String name;
	private String phoneNum;
	private String telephonNum;
	private String email;
	private String address;
	private String sortKey;

	public String getSortKey() {
		return sortKey;
	}

	public void setSortKey(String sortKey) {
		this.sortKey = sortKey;
	}

	public String getContactId() {
		return contactId;
	}

	public void setContactId(String contactId) {
		this.contactId = contactId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public String getTelephonNum() {
		return telephonNum;
	}

	public void setTelephonNum(String telephonNum) {
		this.telephonNum = telephonNum;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值