【Android】获取手机通讯录中的联系人信息

1.手机中的联系人信息是经常用到的一个功能,下面这段代码实现了获取联系人信息的功能

private List initContactList() {
		List contactList = new ArrayList();

		// 查询联系人数据
		Cursor cursor = 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));
			// 获取联系人的姓名
			String contactName = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			contact.setContactName(contactName);

			// 有联系人姓名得到对应的拼音
			String pinyin = PinyinUtils.getPinyin(contactName);
			contact.setPinyin(pinyin);

			Cursor phoneCursor = getContentResolver().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
							+ contactId, null, null);

			while (phoneCursor.moveToNext()) {
				String phoneNumber = phoneCursor
						.getString(phoneCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				contact.setPhoneNumber(phoneNumber);
			}

			if (null != phoneCursor && !phoneCursor.isClosed()) {
				phoneCursor.close();
			}

			contactList.add(contact);
		}

		if (null != cursor && !cursor.isClosed()) {
			cursor.close();
		}

		Log.i("", contactList.toString());

		return contactList;
	}








参考:http://fanfq.iteye.com/blog/779569



你可能感兴趣的:(Android)