Android 获取Contacts 联系人 姓名 号码 照片信息

简介: 直接贴代码不解释 private void getCursors() { Cursor phoneCursor = this.managedQuery( ContactsContract.
直接贴代码不解释
private void getCursors() {
		Cursor phoneCursor = this.managedQuery(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
				null, null);
		Cursor callLogCursor = this.managedQuery(CallLog.Calls.CONTENT_URI ,
				new String[] { CallLog.Calls.NUMBER,
						CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE },
						 "1=1) group by "+CallLog.Calls.NUMBER+" -- (", null, null);

		// update listshow
		if (0 < phoneCursor.getCount()||0 < callLogCursor.getCount()) {
			updateCursorList(phoneCursor,callLogCursor);
		}
	}



private void updateCursorList(Cursor phoneCursor,Cursor callLogCursor) {
		Cursor contactCursor = null;
		customArrayList.clear();
		Log.i(TAG, "callLogCursor.getCount()" + callLogCursor.getCount());
		if(0 < callLogCursor.getCount()){
			callLogCursor.moveToFirst();
			while (callLogCursor.getPosition() != callLogCursor.getCount()) {
				String number = callLogCursor.getString(callLogCursor
						.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
				String name = callLogCursor.getString(callLogCursor
						.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));
				if(null == name){
					ContactEntity contactentity = new ContactEntity();
					contactentity.contacts_display_name = number;
					contactentity.contacts_phone_number = number;
					customArrayList.add(contactentity);
				}
				callLogCursor.moveToNext();
			}
		}

		Log.i(TAG, "phoneCursor.getCount()" + phoneCursor.getCount());

		if(0 < phoneCursor.getCount()){
		phoneCursor.moveToFirst();
		// find all contact list
		while (phoneCursor.getPosition() != phoneCursor.getCount()) {
			ContactEntity contactentity = new ContactEntity();
			contactentity.contact_id = phoneCursor
					.getLong(phoneCursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
			contactentity.contacts_phone_type = phoneCursor
					.getInt(phoneCursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
			contactentity.contacts_phone_number = phoneCursor
					.getString(phoneCursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(
									"-", "");

			contactCursor = this.managedQuery(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + "="
							+ contactentity.contact_id, null, null);
			contactCursor.moveToFirst();
			contactentity.contacts_display_name = contactCursor
					.getString(contactCursor
							.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).replace(
									"-", "");
//			contactentity.contact_photo_id = contactCursor.getInt(contactCursor
//					.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
//			if (0 != contactentity.contact_photo_id) {
//				Uri peopleURI = ContentUris.withAppendedId(People.CONTENT_URI,
//						new Long(contactentity.contact_photo_id));
//				contactentity.contact_phone_bmp = People.loadContactPhoto(this,
//						peopleURI, 0, null);
//			}

			Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
				            contactentity.contact_id);
			InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);
			if(null != input){
			contactentity.contact_phone_bmp = BitmapFactory.decodeStream(input);
			}

			// spell name can
			contactentity.spellName = PinYin.getInstance(this).getPinyinString(
					contactentity.contacts_display_name);

			Log.i(TAG, "contactentity.contact_id: " + contactentity.contact_id
					+ " contactentity.contacts_phone_type: "
					+ contactentity.contacts_phone_type
					+ " contactentity.contacts_phone_number: "
					+ contactentity.contacts_phone_number
					+ "contactentity.contacts_display_name: "
					+ contactentity.contacts_display_name
					+ "contactentity.contact_phone_bmp: "
					+ contactentity.contact_phone_bmp);
			phoneCursor.moveToNext();

			customArrayList.add(contactentity);
		}
		}

		Collections.sort(customArrayList, new Comparator<ContactEntity>() {
			public int compare(ContactEntity o1, ContactEntity o2) {
				ContactEntity he1 = o1, he2 = o2;
				return he1.getName(ContactEntity.SPELL_NAME)
						.compareToIgnoreCase(
								he2.getName(ContactEntity.SPELL_NAME));
			}
		});

		if (null != phoneCursor|| null!= callLogCursor) {
			if (null != mAdapter) {
				mAdapter.list = customArrayList;
				mAdapter.notifyDataSetChanged();
			} else {
				mAdapter = new ContactAdapter(customArrayList);
				mContactList.setAdapter(mAdapter);
			}
			try {
				if(0 < phoneCursor.getCount()){
				phoneCursor.close();
				contactCursor.close();
				}
				if(0 < callLogCursor.getCount()){
				callLogCursor.close();
				}
			} catch (Exception e) {
				Log.e(TAG, e.getMessage());
				// ignore
			}
		}
	}
目录
打赏
0
0
0
0
86
分享
相关文章
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
1360 0
|
6月前
|
Android Studio中Terminal运行./gradlew clean build提示错误信息
遇到 `./gradlew clean build`命令执行出错时,首先应检查错误信息的具体内容,这通常会指向问题的根源。从权限、环境配置、依赖下载、版本兼容性到项目配置本身,逐一排查并应用相应的解决措施。记住,保持耐心,逐步解决问题,往往复杂问题都是由简单原因引起的。
597 2
【苹果安卓通用】xlsx 和 vCard 文件转换器,txt转vCard文件格式,CSV转 vCard格式,如何批量号码导入手机通讯录,一篇文章说全
本文介绍了如何快速将批量号码导入手机通讯录,适用于企业客户管理、营销团队、活动组织、团队协作和新员工入职等场景。步骤包括:1) 下载软件,提供腾讯云盘和百度网盘链接;2) 打开软件,复制粘贴号码并进行加载预览和制作文件;3) 将制作好的文件通过QQ或微信发送至手机,然后按苹果、安卓或鸿蒙系统的指示导入。整个过程简便快捷,可在1分钟内完成。
269 6
【通讯录教程】苹果安卓鸿蒙系统通用,如何大批量导入手机号码到手机的通讯录,下面教你方法,只需1分钟搞定几万个号码的导入手机电话本
该文介绍了一种快速批量导入手机通讯录的方法,适用于处理大量手机号的需求,如微商管理、客户资料整理等。在QQ同步助手开始收费后,提供了免费的替代方案。步骤包括:下载批量导入软件(链接提供腾讯云盘和百度网盘地址),清空通讯录(非必需),制作符合格式的通讯录文件,并按操作系统(苹果、安卓或鸿蒙)进行导入。整个过程只需1分钟,简便快捷。
958 2
详细解读Android获取已安装应用信息(图标,名称,版本号,包)
详细解读Android获取已安装应用信息(图标,名称,版本号,包)
169 0
Android通讯录开发之通讯录联系人搜索功能最新实现
Android通讯录开发之通讯录联系人搜索功能最新实现
android 修改kernel编译版本信息
android 修改kernel编译版本信息
112 0
|
11月前
|
Android 获取签名信息
Android 获取签名信息
99 0
android mk中打印信息
android mk中打印信息
60 0

热门文章

最新文章