Android联系人的一些读取操作-创新互联
源码来自我的一个拨号应用:https://github.com/NashLegend/QuicKid
我们拥有10余年网页设计和网站建设经验,从网站策划到网站制作,我们的网页设计师为您提供的解决方案。为企业提供成都做网站、网站设计、微信开发、成都小程序开发、成都做手机网站、H5技术、等业务。无论您有什么样的网站设计或者设计方案要求,我们都将富于创造性的提供专业设计服务并满足您的需求。1.读取带电话号码的所有联系人。
Android系统貌似没有直接取得带电话号码的联系人列表的功能。直接读取Contacts.CONTENT_URI只能读取联系人信息却得不到电话号码。如果先读取联系人列表,再通过联系人列表一个一个读取电话号码又非常慢,所以可以这样读:先从Phone.CONTENT_URI读取出电话列表,但是有可能一个人对应多个号码,这时只要合并一下就可以了,根据Contacts.SORT_KEY_PRIMARY排序,同一个人的不同号码是靠在一起的,这样合并就变得非常容易,坏处是对于没有电话号码的联系人这里是取不到的。(Contact是一个自定义类,这里没有写出来,这不是重点……)
public static void loadContacts(Context context) { ArrayListAllContacts = new ArrayList (); ContentResolver resolver = context.getContentResolver(); // 要使用RawContacts.CONTACT_ID而不是Contacts.CONTACT_ID String[] PROJECTION = { RawContacts.CONTACT_ID, Contacts.DISPLAY_NAME, Contacts.LOOKUP_KEY, Contacts.PHOTO_THUMBNAIL_URI, Phone.NUMBER, Phone.TYPE, Contacts.STARRED }; Cursor cursor = resolver.query(Phone.CONTENT_URI, PROJECTION, null, null, Contacts.SORT_KEY_PRIMARY); String preLookupKey = ""; Contact preContact = null; if (cursor.moveToFirst()) { do { long contractID = cursor.getInt(0); String displayName = cursor.getString(1); String lookupKey = cursor.getString(2); String photoUri = cursor.getString(3); boolean starred = cursor.getInt(6) == 1; if (lookupKey.equals(preLookupKey) && preContact != null) { preContact.addPhone(cursor.getString(4), cursor.getInt(5)); } else { Contact contact = new Contact(); contact.setContactId(contractID); contact.setName(displayName); contact.setLookupKey(lookupKey); contact.setPhotoUri(photoUri); contact.addPhone(cursor.getString(4), cursor.getInt(5)); contact.setStarred(starred); AllContacts.add(contact); preLookupKey = lookupKey; preContact = contact; } } while (cursor.moveToNext()); } else { // No Phone Number Found } cursor.close(); }
2.读取最近联系人
Android可以通过查询Contacts.CONTENT_STREQUENT_URI得到最近联系人。注意这里得到的不是历史通话记录,而是系统根据通话频率自动获得的。
public static void loadStrequent() { ArrayListStrequentContacts = new ArrayList (); String[] projection = { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.LOOKUP_KEY, Contacts.PHOTO_THUMBNAIL_URI, Contacts.TIMES_CONTACTED, Contacts.LAST_TIME_CONTACTED, Contacts.STARRED, Contacts.PHOTO_ID }; ContentResolver resolver = AppApplication.globalApplication .getContentResolver(); // 显示最近联系人和收藏的联系人 Cursor cursor = resolver.query(Contacts.CONTENT_STREQUENT_URI, projection, null, null, null); // 加载最近联系人,不包括收藏的联系人 //Cursor cursor = resolver.query( // Uri.withAppendedPath(Contacts.CONTENT_URI, "frequent"), // projection, null, null, null); while (cursor.moveToNext()) { Contact contact = new Contact(); long contractID = cursor.getInt(0); String displayName = cursor.getString(1); String lookupKey = cursor.getString(2); String photoUri = cursor.getString(3); int TIMES_CONTACTED = cursor.getInt(4); long LAST_TIME_CONTACTED = cursor.getLong(5); boolean starred = cursor.getInt(6) == 1; contact.setContactId(contractID); contact.setName(displayName); contact.setLookupKey(lookupKey); contact.setPhotoUri(photoUri); contact.setStarred(starred); contact.Times_Contacted = TIMES_CONTACTED; contact.Last_Time_Contacted = LAST_TIME_CONTACTED; StrequentContacts.add(contact); } cursor.close(); // notify }
3.读取合并后的通话记录。
查询Calls.CONTENT_URI,并不能读取出具体的联系人信息,如果要知道最近通话记录,要跟已经联系人列表对照使用。
public static void loadCallLogsCombined() { if (AllContacts.size() == 0) { loadContacts(); } ArrayListrecentContacts = new ArrayList (); String[] projection = { Calls._ID, Calls.TYPE, Calls.CACHED_NAME, Calls.CACHED_NUMBER_TYPE, Calls.DATE, Calls.DURATION, Calls.NUMBER }; ContentResolver resolver = AppApplication.globalApplication .getContentResolver(); Cursor cursor = resolver.query(Calls.CONTENT_URI, projection, null, null, Calls.DEFAULT_SORT_ORDER); while (cursor.moveToNext()) { long callID = cursor.getInt(0); int callType = cursor.getInt(1); String name = cursor.getString(2); int numberType = cursor.getInt(3); long date = cursor.getLong(4); int duration = cursor.getInt(5); String number = cursor.getString(6); if (TextUtils.isEmpty(name)) { boolean matched = false; for (Iterator iterator = recentContacts.iterator(); iterator .hasNext();) { Contact con = iterator.next(); if (con.Last_Contact_Number.equals(number)) { matched = true; con.Times_Contacted++; break; } } if (!matched) { Contact tmpContact = new Contact(); tmpContact.Times_Contacted = 1; tmpContact.Last_Contact_Call_ID = callID; tmpContact.Last_Contact_Call_Type = callType; tmpContact.Last_Contact_Number = number; tmpContact.Last_Contact_Phone_Type = numberType; tmpContact.Last_Time_Contacted = date; tmpContact.Last_Contact_Duration = duration; recentContacts.add(tmpContact); } } else { boolean matched = false; for (Iterator iterator = recentContacts.iterator(); iterator .hasNext();) { Contact con = iterator.next(); if (con.Last_Contact_Number.equals(number)) { matched = true; con.Times_Contacted++; break; } } if (!matched) { match3: for (Iterator iterator = AllContacts .iterator(); iterator.hasNext();) { Contact con = iterator.next(); ArrayList phones = con.getPhones(); for (Iterator iterator2 = phones .iterator(); iterator2.hasNext();) { PhoneStruct phoneStruct = iterator2.next(); if (phoneStruct.phoneNumber.equals(number)) { matched = true; Contact tmpContact = con.clone(); tmpContact .setPhones(new ArrayList ()); tmpContact.Times_Contacted = 1; tmpContact.Last_Contact_Call_ID = callID; tmpContact.Last_Contact_Call_Type = callType; tmpContact.Last_Contact_Number = number; tmpContact.Last_Contact_Phone_Type = numberType; tmpContact.Last_Time_Contacted = date; tmpContact.Last_Contact_Duration = duration; recentContacts.add(tmpContact); break match3; } } } } } } cursor.close(); }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前文章:Android联系人的一些读取操作-创新互联
当前路径:http://azwzsj.com/article/dpdssh.html