android总结查询联系人和短信内容


	/**
	 * 获取所有联系人
	 * 
	 * @param context
	 * @return
	 */
	public static List<ContactBean> getContacts(Context context) {
		List<ContactBean> cb = new ArrayList<>();
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			do {
				ContactBean bean = new ContactBean();
				int columnIndexDISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
				int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
				int _ID = cursor.getInt(columnIndex_ID);
				String displayName = cursor.getString(columnIndexDISPLAY_NAME);
				bean.set_ID(_ID);
				bean.setName(displayName);
				List<String> phonesList = new ArrayList<>();
				// 获取联系人手机号码
				Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + _ID, null, null);
				if (phoneCursor != null && phoneCursor.moveToFirst()) {
					do {
						String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						phonesList.add(phone);
					} while (phoneCursor.moveToNext());
					phoneCursor.close();
				}
				bean.setPhone(phonesList);
				String uriPhoto = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
				bean.setUriPhoto(uriPhoto);
				cb.add(bean);
			} while (cursor.moveToNext());
			cursor.close();
		}
		return cb;
	}

	/**
	 * 根据key进行模糊查询
	 * http://www.cnblogs.com/snake-hand/archive/2013/06/05/3119778.html
	 * 
	 * @param key
	 */
	@SuppressLint("InlinedApi")
	public static List<ContactBean> getContactsFuzzyQueryByKey(Context context, String key) {
		List<ContactBean> cb = new ArrayList<>();
		ContentResolver cr = context.getContentResolver();
		String[] projection = { ContactsContract.Contacts._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.PHOTO_URI, ContactsContract.CommonDataKinds.Phone.NUMBER };
		StringBuilder selection = new StringBuilder();
		selection.append(ContactsContract.Contacts.DISPLAY_NAME);
		selection.append(" LIKE '%" + key + "%' ");
		selection.append(" OR ");
		selection.append(ContactsContract.CommonDataKinds.Phone.NUMBER);
		selection.append(" LIKE '%" + key + "%' ");
		Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection.toString(), null, null);
		if (cursor != null && cursor.moveToFirst()) {
			do {
				ContactBean bean = new ContactBean();
				long _id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
				String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				String uriPhoto = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
				bean.set_ID(_id);
				bean.setName(name);
				// bean.setPhone(number);
				bean.setUriPhoto(uriPhoto);
				cb.add(bean);
			} while (cursor.moveToNext());
			cursor.close();
		}
		return cb;
	}

	/**
	 * 通过电话查询联系人
	 * 
	 * @param context
	 * @param phoneNumber
	 * @return
	 */
	public static String lookupNameByPhoneNumber(Context context, String phoneNumber) {
		Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
		Cursor cursor = context.getContentResolver().query(uri, new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
		String name = null;
		if (cursor == null) {
			name = null;
		}
		try {
			if (cursor.moveToFirst()) {
				name = cursor.getString(0);
			}
		} catch (Exception e) {
			name = "";
		} finally {
			cursor.close();
		}
		return name;
	}

	/**
	 * Contacts点击跳转到通讯录界面
	 * 
	 * @param context
	 */
	public static void selectContacts(Activity context, int requestCode) {
		Uri uri = Uri.parse("content://contacts/people");
		Intent intent = new Intent(Intent.ACTION_PICK, uri);
		context.startActivityForResult(intent, requestCode);
	}

	public static void selectContactsOnActivityResult(Context context, int requestCode, int resultCode, Intent data) {
		if (data == null) {
			return;
		}
		Uri uri = data.getData();
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(uri, null, null, null, null);
		if (cursor == null) {
			return;
		}
		if (cursor.moveToFirst()) {
			int columnIndexDISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
			String displayName = cursor.getString(columnIndexDISPLAY_NAME);
			// https://my.oschina.net/moziqi/blog/365727
			cursor.close();
		}
	}

	public static List<MsgBean> getSmsFuzzyQueryByKey(Context context, String key) {
		List<MsgBean> msgs = new ArrayList<>();
		ContentResolver cr = context.getContentResolver();
		String[] projection = { Sms.SEEN };
		StringBuilder selection = new StringBuilder();
		selection.append(Sms.BODY);
		selection.append(" LIKE '%" + key + "%' ");
		Cursor cursor = cr.query(Sms.CONTENT_URI, null, selection.toString(), null, null);
		if (cursor != null && cursor.moveToFirst()) {
			do {
				MsgBean bean = new MsgBean();
				long _ID = cursor.getLong(cursor.getColumnIndex(Sms._ID));
				String body = cursor.getString(cursor.getColumnIndex(Sms.BODY));
				int type = cursor.getInt(cursor.getColumnIndex(Sms.TYPE));
				int thread_id = cursor.getInt(cursor.getColumnIndex(Sms.THREAD_ID));
				int person = cursor.getInt(cursor.getColumnIndex(Sms.PERSON));
				String address = cursor.getString(cursor.getColumnIndex(Sms.ADDRESS));
				long date = cursor.getLong(cursor.getColumnIndex(Sms.DATE));
				long date_sent = cursor.getLong(cursor.getColumnIndex(Sms.DATE_SENT));
				bean.set_ID(_ID);
				bean.setContent(body);
				bean.setType(type);
				bean.setPersonID(person);
				bean.setAddress(address);
				bean.setDate(date);
				bean.setDate_sent(date_sent);
				bean.setThread_id(thread_id);
				//通过地址查询名字
				//http://stackoverflow.com/questions/35112734/how-to-read-contact-name-from-inbox-messages
				final String[] phoneProjection = new String[] { ContactsContract.PhoneLookup.PHOTO_URI, ContactsContract.Data.DISPLAY_NAME };
				Cursor contactCursor = null;
				try {
					Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
					contactCursor = cr.query(uri, phoneProjection, null, null, null);
					if (contactCursor != null && contactCursor.moveToFirst()) {
						String name  = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
						String uriPhoto = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
						bean.setUriPhoto(uriPhoto);// 设置头像
						bean.setAddress(name);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (contactCursor != null) {
						contactCursor.close();
					}
				}
				msgs.add(bean);
			} while (cursor.moveToNext());
			cursor.close();
		}
		return msgs;
	}


    holderContact.contentRelativeLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 打开联系人
					//http://stackoverflow.com/questions/4275167/how-to-open-a-contact-card-in-android-by-id
					try {
						if (contactBean.get_ID() > 0) {
							Intent defineIntent = new Intent(Intent.ACTION_VIEW);
							Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactBean.get_ID()));
							defineIntent.setData(uri);
							defineIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							context.startActivity(defineIntent);
						}
					} catch (Exception e) {
						LogDebug.e(context, e.getMessage());
					}
				}
			});


     holderMessage.contentRelativeLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					try {
						// http://stackoverflow.com/questions/1412221/how-to-open-up-a-specific-sms-in-android
						Intent defineIntent = new Intent(Intent.ACTION_VIEW);
						defineIntent.setData(Uri.parse("content://mms-sms/conversations/" + msgBean.getThread_id()));
						defineIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(defineIntent);
					} catch (Exception e) {
					}
				}
			});

 

转载于:https://my.oschina.net/moziqi/blog/793095

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值