/** * 获取库Phon表字段 只去表中找 显示名称 电话号码 这2个数据 **/ private static final String[] PHONES_PROJECTION = new String[]{
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.CONTACT_ID};
/** * 联系人显示名称 **/ private static final int PHONES_DISPLAY_NAME_INDEX = 0; /** * 电话号码 **/ private static final int PHONES_NUMBER_INDEX = 1; /** * 联系人ID **/ private static final int PHONES_ID_INDEX = 2;
/** * 获取手机通讯录联系人 返回有号码联系人列表 * * @param context * @return 有号码的通讯录成员列表 模型:Contact */ public static List<Contact> getPhoneContacts(Context context) { List<Contact> contactList = new ArrayList<>(); ContentResolver resolver = context.getContentResolver(); // 获取手机联系人,手机号,姓名和联系人id三列数据 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); Cursor emails = null; Cursor address = null; Cursor postNameCursor = null; if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); phoneNumber = phoneNumber.replaceAll(" ", ""); // 当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) continue; // // 得到联系人名称 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); Contact tmp = new Contact(); if (!TextUtils.isEmpty(contactName)) { tmp.setUser_name(contactName); } else { tmp.setUser_name(phoneNumber); } tmp.setMobile(phoneNumber);
// 根据id来查邮箱 这里取得是第一条数据
String contactid = phoneCursor.getString(PHONES_ID_INDEX); // 获取该联系人邮箱 emails = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactid, null, null); if (emails != null && emails.moveToFirst()) { String emailType = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); String emailValue = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); Log.i("emailType", emailType + ""); Log.i("emailValue", emailValue + ""); if (emailValue != null) { tmp.setEmail(emailValue); } else { tmp.setEmail(""); } } // 获取该联系人地址
// 根据id来查地址 这里取得是第一条数据address = context.getContentResolver().query( ContactsContract.CommonDataKinds.StructuredPostal. CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone. CONTACT_ID + " = " + contactid, null, null); if (address != null && address.moveToFirst()) { // 地址 String formatAddress = address .getString(address .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal. FORMATTED_ADDRESS)); Log. i( "formatAddress", formatAddress + ""); if (formatAddress != null) { tmp.setAddress(formatAddress); } else { tmp.setAddress( ""); } } // 获取职位从组织中获取 查找的指定id的职位 postNameCursor = context.getContentResolver().query(ContactsContract.Data. CONTENT_URI, null, "mimetype = ? and " + ContactsContract.CommonDataKinds.Phone. CONTACT_ID + " = ?", new String[]{ContactsContract.CommonDataKinds.Organization. CONTENT_ITEM_TYPE,contactid}, null); if (postNameCursor != null && postNameCursor.moveToFirst()) { // 职位 String postName = postNameCursor .getString(postNameCursor .getColumnIndex(ContactsContract.CommonDataKinds.Organization. TITLE)); Log. i( "postName", postName + ""); if (postName != null) { tmp.setComduty(postName); } else { tmp.setComduty( ""); } } contactList.add(tmp); // 转化为自己的对象列表 } phoneCursor.close(); if (emails != null) { emails.close(); } if (address != null) { address.close(); } if (postNameCursor != null) { phoneCursor.close(); } } return contactList;}
以上方式读取时由于循环查询数据库,会出现加载特别慢的情况。
改进措施,先查询所有数据。然后通过循环比对id封装数据。
/** * 获取库Phon表字段 只去表中找 显示名称 电话号码 这2个数据 **/ private static final String[] PHONES_PROJECTION = new String[]{ Phone.DISPLAY_NAME, Phone.NUMBER, Phone.CONTACT_ID}; private static final String[] EMAIL_PROJECTION = new String[]{ Email.TYPE, Email.DATA, Phone.CONTACT_ID}; private static final String[] ADDRESS_PROJECTION = new String[]{ StructuredPostal.FORMATTED_ADDRESS, Phone.CONTACT_ID}; private static final String[] OGNIZATION_PROJECTION = new String[]{ Organization.TITLE, Phone.CONTACT_ID};
// 获取手机联系人,手机号,姓名和联系人id三列数据 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); Cursor emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, EMAIL_PROJECTION, null, null, null); Cursor address = resolver.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, ADDRESS_PROJECTION, null, null, null); Cursor postNameCursor = resolver.query(ContactsContract.Data.CONTENT_URI, OGNIZATION_PROJECTION, "mimetype = ?", new String[]{ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}, null);
/** * 获取手机通讯录联系人 * * @param context * @return 有号码的通讯录成员列表 模型:member */ public static ListgetPhoneContacts(Context context) { List contactList = new ArrayList (); ContentResolver resolver = context.getContentResolver(); // 获取手机联系人,手机号,姓名和联系人id三列数据 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); Cursor emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, EMAIL_PROJECTION, null, null, null); Cursor address = resolver.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, ADDRESS_PROJECTION, null, null, null); Cursor postNameCursor = resolver.query(ContactsContract.Data.CONTENT_URI, OGNIZATION_PROJECTION, "mimetype = ?", new String[]{ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); phoneNumber = phoneNumber.replaceAll(" ", ""); // 当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) continue; // // 得到联系人名称 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); Member tmp = new Member(); if (!TextUtils.isEmpty(contactName)) { tmp.setUser_name(contactName); } else { tmp.setUser_name(phoneNumber); } tmp.setMobile(phoneNumber); // 设置联系人首字母 setUserHearder(tmp); String contactid = phoneCursor.getString(PHONES_ID_INDEX); // 设置邮箱 if (emails != null) { if (emails.moveToFirst()) { do { String id = emails.getString(emails.getColumnIndex(Phone.CONTACT_ID)); if (contactid.equals(id)) { String emailValue = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); if (emailValue != null) { tmp.setEmail(emailValue); } else { tmp.setEmail(""); } break; } } while (emails.moveToNext()); } } // 获取该联系人邮箱 // emails = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, // ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactid, null, null); // if (emails != null && emails.moveToFirst()) { // // 遍历所有的电话号码 // String emailType = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); // String emailValue = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); // Log.i("emailType", emailType + ""); // Log.i("emailValue", emailValue + ""); // if (emailValue != null) { // tmp.setEmail(emailValue); // } else { // tmp.setEmail(""); // } // } // 设置地址 if (address != null) { if (address.moveToFirst()) { do { String id = address.getString(address.getColumnIndex(Phone.CONTACT_ID)); if (contactid.equals(id)) { String formatAddress = address .getString(address .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)); Log.i("formatAddress", formatAddress + ""); if (formatAddress != null) { tmp.setAddress(formatAddress); } else { tmp.setAddress(""); } break; } } while (address.moveToNext()); } } // 获取该联系人地址 // address = context.getContentResolver().query( // ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, // null, // ContactsContract.CommonDataKinds.Phone.CONTACT_ID // + " = " + contactid, null, null); // if (address != null && address.moveToFirst()) { // // 地址 // String formatAddress = address // .getString(address // .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)); // Log.i("formatAddress", formatAddress + ""); // if (formatAddress != null) { // tmp.setAddress(formatAddress); // } else { // tmp.setAddress(""); // } // } // 设置职位 if (postNameCursor != null) { if (postNameCursor.moveToFirst()) { do { String id = postNameCursor.getString(postNameCursor.getColumnIndex(Phone.CONTACT_ID)); if (contactid.equals(id)) { // 职位 String postName = postNameCursor .getString(postNameCursor .getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)); Log.i("postName", postName + ""); if (postName != null) { tmp.setComduty(postName); } else { tmp.setComduty(""); } break; } } while (postNameCursor.moveToNext()); } } // 获取职位从组织中获取 // postNameCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, // null, // "mimetype = ? and " + ContactsContract.CommonDataKinds.Phone.CONTACT_ID // + " = ?", new String[]{ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, contactid}, null); // if (postNameCursor != null && postNameCursor.moveToFirst()) { // // 职位 // String postName = postNameCursor // .getString(postNameCursor // .getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)); // Log.i("postName", postName + ""); // if (postName != null) { // tmp.setComduty(postName); // } else { // tmp.setComduty(""); // } // } contactList.add(tmp); } phoneCursor.close(); } if (emails != null) { emails.close(); } if (address != null) { address.close(); } if (postNameCursor != null) { postNameCursor.close(); } // 按首字母和姓名排序 if (contactList.size() > 0) { sortByAlphaname(contactList); } return contactList; }
android 获取联系人信息:
http://www.soso.io/article/99176.html