import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.ContactsContract; import android.text.TextUtils; import com.app.common.util.Log; import com.gzshapp.gzsh.R; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * 通讯录控制类 1、获取手机通讯录联系人信息 2、获取手机sim卡联系人信息 */ public class ContactsController { private final String TAG = "Contact"; /** * 获取库Phone表字段 */ private static final String[] PHONES_PROJECTION = new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Photo.PHOTO_ID, ContactsContract.CommonDataKinds.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_PHOTO_ID_INDEX = 2; /** * 联系人的ID* */ private static final int PHONES_CONTACT_ID_INDEX = 3; private Context mContext; public ContactsController(Context mContext) { this.mContext = mContext; } /** * 获取到全部的手机联系人 */ public List<ContactsModel> getContact() { // 联系人列表 List<ContactsModel> contactsList = new ArrayList<ContactsModel>(); //获得所有的联系人 Cursor cur = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); //循环遍历 if (cur.moveToFirst()) { int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID); int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); do { ContactsModel model = new ContactsModel(); List<String> phoneNumbers = new ArrayList<String>(); //获得联系人的ID号 String contactId = cur.getString(idColumn); model.setContactId(Long.valueOf(contactId)); //获得联系人姓名 String contactName = cur.getString(displayNameColumn); model.setContactName(contactName); //查看该联系人有多少个电话号码。如果没有这返回值为0 int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (phoneCount > 0) { //获得联系人的电话号码 Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); if (phones.moveToFirst()) { do { //遍历所有的电话号码 String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); phoneNumbers.add(formatPhoneNumber(phoneNumber)); model.setPhoneNumbers(phoneNumbers); // System.out.println(phoneNumber); } while (phones.moveToNext()); } } contactsList.add(model); } while (cur.moveToNext()); } if (null != cur) { cur.close(); } //打印 // print(contactsList); Log.e(TAG, "手机通讯录共有" + contactsList.size() + "条记录"); return contactsList; } private void print(List<ContactsModel> contactsList) { for (int i = 0; i < contactsList.size(); i++) { String msg = contactsList.get(i).getContactName() + ":"; List<String> phones = contactsList.get(i).getPhoneNumbers(); String phone = ""; if (null != phones && phones.size() > 0) { for (int j = 0; j < phones.size(); j++) { phone += phones.get(j) + ","; } } msg += phone; System.out.println(msg); } } /** * 得到手机SIM卡联系人人信息 */ public List<ContactsModel> getSIMContacts() { // 联系人列表 List<ContactsModel> contactsList = new ArrayList<ContactsModel>(); ContentResolver resolver = mContext.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 phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); // 当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) continue; // 得到联系人名称 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); // Sim卡中没有联系人头像 ContactsModel model = new ContactsModel(); List<String> phoneNumbers = new ArrayList<String>(); phoneNumbers.add(phoneNumber); model.setContactName(contactName); model.setPhoneNumbers(phoneNumbers); contactsList.add(model); } phoneCursor.close(); } return contactsList; } /** * 格式化手机号码 * * @param phoneNumber * @return */ private String formatPhoneNumber(String phoneNumber) { //去空格 phoneNumber = phoneNumber.replace(" ", "").trim(); //去除+86开头的 if (phoneNumber.startsWith("+86")) { phoneNumber = phoneNumber.substring(3); } //去除- if (phoneNumber.contains("-")) { phoneNumber = phoneNumber.replace("-", "").trim(); } //去除区号 if (phoneNumber.startsWith("0") && (phoneNumber.length() == 11 || phoneNumber.length() == 12)) { phoneNumber = phoneNumber.substring(4); } return phoneNumber; } /** * 去除手机号码重复的 * * @param contactsList * @return */ private List<ContactsModel> megerContactsList(List<ContactsModel> contactsList) { for (int i = 0; i < contactsList.size(); i++) { ContactsModel model = contactsList.get(i); for (int j = contactsList.size() - 1; j > 0; j--) { ContactsModel tempModel = contactsList.get(j); if (null != tempModel && null != model) { List<String> phones = model.getPhoneNumbers(); List<String> tempPhones = tempModel.getPhoneNumbers(); if (null != phones && phones.size() > 0 && null != tempPhones && tempPhones.size() > 0) if (phones.get(0).equals(tempPhones.get(0))) { contactsList.remove(j); } } } } return contactsList; } }
时间: 2024-10-13 04:16:57