去重并且合并联系人
来源:互联网 发布:俄罗斯社交软件vk 编辑:程序博客网 时间:2024/05/16 05:12
/** * 获取系统联系人的信息并排序 */ public ArrayList<ContactInfo> getContacts() { ArrayList<ContactInfo> listsContacts = new ArrayList<>(); ContactInfo member = null; ArrayList<String> contactPhones = new ArrayList<>(); Cursor cursor = null; Cursor noteCur = null; ArrayList<ContactInfo> tempContacts = new ArrayList<>();; try { long oldContactId = -1;// Uri uri = ContactsContractOutUri.CommonDataKindPhoneContentUri; Uri uri = Uri.parse("content://com.android.contacts"); Uri dataUri = Uri.withAppendedPath(uri, "data"); Uri commonPhoneUri = Uri.withAppendedPath(dataUri, "phones"); cursor = context.getContentResolver().query(commonPhoneUri, contact_projection, null, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String oldSortKey = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY)); String newSortKey = oldSortKey; if (isChinese(oldSortKey.charAt(0))) { newSortKey = CharacterParser.getInstance().getSelling(oldSortKey); } String sortKey = getSortKey(newSortKey); long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (contactId == oldContactId) { contactPhones.add(number); member.setContactPhones(contactPhones); continue; } else { member = new ContactInfo(); contactPhones = new ArrayList<String>(); contactPhones.add(number); } int photoId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID)); /** 头像*/ if (photoId != 0) { Uri photoUri = Uri.parse("content://com.android.contacts/contacts/" + contactId); InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(context.getContentResolver(), photoUri); byte[] photoByte = null; if (input != null) { photoByte = toByteArray(input); member.setPhoto(photoByte); } } String note = ""; String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] noteWhereParams = new String[]{"" + contactId, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; Uri noteBaseUri = Uri.parse("content://com.android.contacts"); Uri noteUri = Uri.withAppendedPath(noteBaseUri, "data"); noteCur = context.getContentResolver().query(noteUri, null, noteWhere, noteWhereParams, null); if (noteCur != null && noteCur.moveToFirst()) { note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)); } member.setRemark(note); member.setSortKey(sortKey); member.setContactPhones(contactPhones); member.setName(name); member.setContactId(contactId); boolean needAdd = true; for(ContactInfo tempContactInfo:tempContacts){ String infoName = tempContactInfo.getName(); String memberName = member.getName(); if(!infoName.equalsIgnoreCase(memberName)){ needAdd = true; break; } List<String> infoPhones = tempContactInfo.getPhones(); List<String> memberPhones = member.getPhones(); if(infoPhones.size() != memberPhones.size() || !infoPhones.containsAll(memberPhones)){ needAdd = true; break; } needAdd = false; byte[] infoIcon = tempContactInfo.getPhoto(); byte[] memberIcon = member.getPhoto(); if(infoIcon == null || infoIcon.length == 0 && memberIcon != null && memberIcon.length > 0){ tempContactInfo.setPhoto(memberIcon); } String infoRemark = tempContactInfo.getRemark(); String memberReamrk = member.getRemark(); if(TextUtils.isEmpty(infoRemark) && !TextUtils.isEmpty(memberReamrk)){ tempContactInfo.setRemark(memberReamrk); } } if(needAdd){ tempContacts.add(member); } oldContactId = contactId; } } for (ContactInfo c: tempContacts){ if (!ApplicationSQLManger.getInstance().insertContactIdIsExistInLauncher(c)) { listsContacts.add(c); } } if (listsContacts != null) { Collections.sort(listsContacts, new Pycomparator()); } } catch (Exception e) { LogUtils.i("ContactsManager", "Exception"); e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } if (noteCur != null) { noteCur.close(); } } return listsContacts; }
0 0
- 去重并且合并联系人
- 两个List<Model>集合合并 并且去重方式
- 联系人去重
- NSMutableArray 合并,去重
- 转载和积累系列 - 合并文件,排序并且去重 linux命令
- 联系人去重的写法优化
- Linux 文件合并去重
- Java List合并去重
- linux文件合并去重
- linux文件合并、去重
- JAVA List<> 合并去重
- shell合并文件去重
- linux文件合并,去重
- 单链表的合并【去重】
- 合并list并去重
- 两个List合并去重
- 安卓 短信去重(看好了,是短信去重,不是联系人去重)
- 字符串比较大小 并且去重
- [交易策略]单均线模型回测
- 二维码的生成细节和原理
- nginx 匹配配置文件启动
- 第12章课后练习5
- 手机滑动分页
- 去重并且合并联系人
- Android进程调度cgroups的简单介绍
- 时间、延迟及延缓操作
- Android Hawk数据库 github开源项目
- Spring MVC 同样拦截器。
- 【iOS】被忽略的main函数
- TextView显示图片,阴影,字体加粗,多种颜色字体,自定义字体
- python Lintcode 刷题(一)
- GradientDrawable和AttributeSet自定义控件