获取通讯录联系人信息

在这里我们先创建一个联系人类,包含人名,电话号码,头像等,当然你还可以包含别的,这里只是一个例子

public class MyContacts {
	//联系人电话号码
	private String phoneNumber;
	//联系人名称
	private String phoneName;
	//联系人头像
	Bitmap contactPhoto;

	//。。。。。。set/get方法。。。。。
}
然后在AndroidManifest.xml中设置权限
读取联系人权限: <uses-permission android:name="android.permission.READ_CONTACTS" />
读写联系人权限: <uses-permission android:name="android.permission.WRITE_CONTACTS" />


然后是方法,获取联系人的姓名,电话号码,头像,返回一个集合

/**

* 得到手机通讯录联系人信息

**/
//这里强调一下query第二个参数PHONES_PROJECTION 下面第10行


//它的意思是只去表中找 显示名称 电话号码 头像ID 联系人ID 这4个数据 ,如果你须要其它数据 的话 就须要修改这里PHONES_PROJECTION。
private static final String[] PHONES_PROJECTION = new String[] {
			Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID };

	/** 得到本机本地联系人信息 **/
	private ArrayList<MyContacts> getPhoneContacts() {
		ArrayList<MyContacts> arrayList = new ArrayList<MyContacts>();
		//获得ContentResolver 实例
		ContentResolver resolver = this.getContentResolver();
		// 获取手机联系人
		Cursor phoneCursor = resolver.query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				PHONES_PROJECTION, null, null, null);
		//判断游标是否为空
		if (phoneCursor != null) {
			while (phoneCursor.moveToNext()) {
				MyContacts myContacts = new MyContacts();
				// 得到手机号码
				String phoneNumber = phoneCursor
						.getString(phoneCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				// 当手机号码为空的或者为空字段 跳过当前循环
				if (TextUtils.isEmpty(phoneNumber))
					continue;

				// 得到联系人名称
				String contactName = phoneCursor
						.getString(phoneCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

				// 得到联系人ID
				Long contactid = phoneCursor
						.getLong(phoneCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

				// 得到联系人头像ID
				Long photoid = phoneCursor
						.getLong(phoneCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.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(getResources(),
							R.drawable.ic_launcher);
				}
				myContacts.setPhoneName(contactName);
				myContacts.setPhoneNumber(phoneNumber);
				myContacts.setContactPhoto(contactPhoto);
				arrayList.add(myContacts);
			}
			phoneCursor.close();
		}
		return arrayList;
	}

	/** 得到手机SIM卡联系人信息 **/
	private ArrayList<MyCardContacts> getSIMContacts() {
		ArrayList<MyCardContacts> list = new ArrayList<MyCardContacts>();
		ContentResolver resolver = this.getContentResolver();
		// 获取Sims卡联系人
		Uri uri = Uri.parse("content://icc/adn");
		Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
				null);

		if (phoneCursor != null) {
			while (phoneCursor.moveToNext()) {
				MyCardContacts cardContacts = new MyCardContacts();
				// 得到手机号码
				String phoneNumber = phoneCursor
						.getString(phoneCursor
								.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
				// 当手机号码为空的或者为空字段 跳过当前循环
				if (TextUtils.isEmpty(phoneNumber))
					continue;
				// 得到联系人名称
				String contactName = phoneCursor
						.getString(phoneCursor
								.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				cardContacts.setName(contactName);
				cardContacts.setNumber(phoneNumber);
				list.add(cardContacts);
			}

			phoneCursor.close();
		}
		return list;
	}


这里写的是代码片段,直接复制到一个方法里就ok了,其他的就是导包了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值