读取通讯录有两种方式,一种方式是参照上篇文章关于android通讯录的数据库表来获取,另一种就是利用系统自带的一种读取通讯录的方式。
先使用后一种简单的方式:
这是获取手机通讯录中的信息。可以根据Phone中对应的字段名区分是哪几张表的数据。实际系统在获取这些通讯录的时候,是通过将几张表关联查询得到的。
/** * 获取所有手机里的通讯录 */ private List<ContactModel> getAllContactsFromPhone() { List<ContactModel> contactList = new ArrayList<ContactModel>(); String[] PHONES_PROJECTION = new String[] { Phone._ID, Phone.RAW_CONTACT_ID, Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.CUSTOM_RINGTONE }; ContentResolver resolver = context.getContentResolver(); // 获取手机联系人 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, Phone.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手机号码 String phoneNumberString = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.NUMBER)); //过滤手机号,如果不需要可以去掉 String phoneNumber = phoneNumberFilter(phoneNumberString); if (phoneNumber != null) { String id = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.RAW_CONTACT_ID)); String contactId = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.CONTACT_ID)); // 得到联系人名称 String contactName = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.DISPLAY_NAME)); String customRingtone = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.CUSTOM_RINGTONE)); ContactModel model = new ContactModel(); model.setId(id); model.setContactId(contactId); model.setDisplayName(contactName); model.setPhoneNumber(phoneNumber); model.setCustomRingtone(customRingtone); contactList.add(model); } } phoneCursor.close(); } return contactList; }
通讯录还包括SIM卡中,接下来是获取sim卡中的通讯录:
/** * 获取所有SIM卡的通讯录 */ private List<ContactModel> getAllContactsFromSIM() { List<ContactModel> contactList = new ArrayList<ContactModel>(); String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }; ContentResolver resolver = context.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()) { // 得到手机号码 String phoneNumberString = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.NUMBER)); String phoneNumber = phoneNumberFilter(phoneNumberString); if (phoneNumber != null) { // 得到联系人名称 String contactName = phoneCursor.getString(phoneCursor .getColumnIndex(Phone.DISPLAY_NAME)); ContactModel model = new ContactModel(); model.setDisplayName(contactName); model.setPhoneNumber(phoneNumber); contactList.add(model); } } phoneCursor.close(); } return contactList; }
sim卡通讯录因为不会存放在手机的数据库中,所以所包含的信息有限。对于上面的方法获取通讯录,都是将通讯录中的所有数据获取,但有的时候我们需要将通讯录进行分组显示,也就是说需要将通讯录中默认的分组信息获取到,显示出来,然后再根据分组来获取里面对应的通讯录信息。这时候就需要通过利用之前讲到的数据库表格来获取了。
第二种,分组获取通讯录:
/** * 获取分组通讯录 * * @return */ private List<ContactGroupModel> getGroupContacts() { int count = 0; ContentResolver resolver = context.getContentResolver(); // 获取所有的groupId,通过groups表 List<ContactGroupModel> contactGroupList = new ArrayList<ContactGroupModel>(); Cursor cursor = resolver.query(ContactsContract.Groups.CONTENT_URI, new String[] { ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }, ContactsContract.Groups.GROUP_VISIBLE + "=?", new String[] { "0" }, ContactsContract.Groups._ID); while (cursor.moveToNext()) { String id = cursor.getString(cursor .getColumnIndex(ContactsContract.Groups._ID)); String title = cursor.getString(cursor .getColumnIndex(ContactsContract.Groups.TITLE)); ContactGroupModel groupModel = new ContactGroupModel(); groupModel.setGroupDisplayName(title); groupModel.setGroupId(id); contactGroupList.add(groupModel); } if (cursor != null) { cursor.close(); } // 根据groupId获取这一组的人员raw_contact_id。根据data表,当获取每个分组所包含的人时,DATA1这个字段代表的含义就是组号,对应的mimetype_id是11 String[] groupRawContactId = new String[] { ContactsContract.Data.RAW_CONTACT_ID }; for (ContactGroupModel groupModel : contactGroupList) { // 获取到这一组中所有人的raw_contact_id Cursor rawContactIdCursor = resolver .query(ContactsContract.Data.CONTENT_URI, groupRawContactId, ContactsContract.Data.DATA1 + "=" + groupModel.getGroupId(), null, null); if (rawContactIdCursor != null) { // 拼成了在这个分组里面的所有人的raw_contact_id StringBuffer sb = new StringBuffer(" IN (0"); while (rawContactIdCursor.moveToNext()) { long rawContactId = rawContactIdCursor.getLong(0); sb.append(",").append(rawContactId); } sb.append(")"); rawContactIdCursor.close(); // 取名字,通过RawContacts表将对应的在上面取到的raw_contact_id对应的用户显示的名称 Map<String, String[]> rawContactIdName = new HashMap<String, String[]>(); Cursor nameCursor = resolver.query( ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID, Phone.DISPLAY_NAME, Phone.CUSTOM_RINGTONE }, ContactsContract.RawContacts._ID + sb.toString(), null, null); if (nameCursor != null) { while (nameCursor.moveToNext()) { String id = nameCursor.getString(0); String contactId = nameCursor.getString(1); String name = nameCursor.getString(2); String customRingtone = nameCursor.getString(3); rawContactIdName.put(id, new String[] { name, contactId, customRingtone }); } nameCursor.close(); } // 取号码,再回到data表中,此时通过上次获取的分组对应的raw_contact_id来取到对应的手机号码 List<ContactModel> contactList = new ArrayList<ContactModel>(); Cursor phoneNumberCursor = resolver.query( ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data._ID, ContactsContract.Data.DATA4, ContactsContract.Data.RAW_CONTACT_ID }, ContactsContract.Data.RAW_CONTACT_ID + sb.toString() + " and " + ContactsContract.Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null, null); if (phoneNumberCursor != null) { while (phoneNumberCursor.moveToNext()) { String phoneNumberString = phoneNumberCursor .getString(1); // 过滤手机号 String phoneNumber = phoneNumberFilter(phoneNumberString); if (phoneNumber != null) { String rawContactId = phoneNumberCursor .getString(2); ContactModel model = new ContactModel(); model.setId(rawContactId); model.setPhoneNumber(phoneNumber); String[] nameRingtone = rawContactIdName .get(rawContactId); model.setDisplayName(nameRingtone[0]); model.setContactId(nameRingtone[1]); model.setCustomRingtone(nameRingtone[2]); contactList.add(model); if (nameRingtone[1] != null && !"".equals(nameRingtone[1])) { System.out.println("个性振铃 :id = " + rawContactId + "|" + phoneNumber + "|" + nameRingtone[0] + "|" + nameRingtone[1]); } } } phoneNumberCursor.close(); } count += contactList.size(); groupModel.setContactList(contactList); } } System.out.println("总数据个数 count = " + count); return contactGroupList; }
上面获取分组通讯录就是利用这样的表关联来完成的,逐个表进行查询,然后取到对应的名字和号码
Android通讯录数据获取
最新推荐文章于 2020-09-04 11:12:04 发布