Android通讯录开发之联系人

获取联系人
  1. 主要数据库表: Data     联系人数据

                           

raw_contacts 表   这个表存储的联系人信息

mimetypes  表  这个表存储的数据类型

具体含义一目了然!!!!!!!  下面上代码!!!!:

 

ContentResolver resolver = context.getContentResolver();
		AllContactAsyncQuery asyncquery = new AllContactAsyncQuery(resolver);
				asyncquery.startQuery(0, null, ContactsContract.Contacts.CONTENT_URI,
				null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY
						+ " COLLATE LOCALIZED ASC");

 

      !!!  这里查询的表 是 contacts 表    这个表里面有photo_id   因为我所做项目需要这个所以查询的这个表!!

 

@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			ArrayList<ContactsModel> list = new ArrayList<ContactsModel>();
			if (cursor == null) {
				return;
			}
			if (cursor.moveToFirst()) {
				do {
					ContactsModel model = new ContactsModel();
					// 获得联系人的ID号
					String contactId = cursor.getLong(cursor
							.getColumnIndex(ContactsContract.Contacts._ID))
							+ "";
					String raw_contactId = cursor.getLong(cursor
							.getColumnIndex("name_raw_contact_id")) + "";
					String namepinyin = cursor
							.getString(cursor
									.getColumnIndex(ContactsContract.Contacts.SORT_KEY_PRIMARY));
					if(namepinyin!=null){
						namepinyin = Contast.DeleteCha(namepinyin);
					}
					int times = cursor
							.getInt(cursor
									.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
					int picid = cursor
							.getInt(cursor
									.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
					// //打电话次数
					// 查看该联系人有多少个电话号码。如果没有这返回值为0
					int phoneCount = cursor
							.getInt(cursor
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

					String number = null;
					byte[] picpath = null;

					String email = null;
					if (phoneCount > 0) {
						// 获得联系人的电话号码
						Cursor phones = context
								.getContentResolver()
								.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = " + contactId, null, null);
						if (phones != null) {

							if (phones.moveToFirst()) {
								number = phones
										.getString(phones
												.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							}
							phones.close();
						}
						// 获取图片路径
						Uri photoUri = ContentUris.withAppendedId(
								ContactsContract.Data.CONTENT_URI, picid);
						phones = context
								.getContentResolver()
								.query(photoUri,
										new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO },
										null, null, null);
						if (phones.moveToFirst()) {
							picpath = phones.getBlob(0);
						}
						phones.close();

						// 获取email
						phones = context
								.getContentResolver()
								.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = " + contactId, null, null);
						if (phones.moveToFirst()) {
							email = phones
									.getString(phones
											.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
						}
						phones.close();
						phones = context
								.getContentResolver()
								.query(ContactsContract.RawContacts.CONTENT_URI,
										null,"_id = " + raw_contactId, null, null);
						// 获得联系人姓名
						String name = cursor
								.getString(cursor
										.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
						phones.close();
						model.setContactID(raw_contactId);
						model.setName(name);
						model.setPhoneNum1(number);
						model.setNamepinyin(namepinyin);
						model.setTimes(times);
						model.setPic(picpath);
						model.setEmail(email);
						list.add(model);
					} else {

					}
				} while (cursor.moveToNext());
				cursor.close();
super.onQueryComplete(token, cookie, cursor);
   }

其余插入更新删除大同小异!!!
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值