源碼來自個人一個撥號應用:https://github.com/NashLegend/QuicKidjava
1.讀取帶電話號碼的全部聯繫人。git
Android系統貌似沒有直接取得帶電話號碼的聯繫人列表的功能。直接讀取Contacts.CONTENT_URI只能讀取聯繫人信息卻得不到電話號碼。若是先讀取聯繫人列表,再經過聯繫人列表一個一個讀取電話號碼又很是慢,因此能夠這樣讀:先從Phone.CONTENT_URI讀取出電話列表,可是有可能一我的對應多個號碼,這時只要合併一下就能夠了,根據Contacts.SORT_KEY_PRIMARY排序,同一我的的不一樣號碼是靠在一塊兒的,這樣合併就變得很是容易,壞處是對於沒有電話號碼的聯繫人這裏是取不到的。(Contact是一個自定義類,這裏沒有寫出來,這不是重點……)github
public static void loadContacts(Context context) { ArrayList<Contact> AllContacts = new ArrayList<Contact>(); 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.讀取最近聯繫人ide
Android能夠經過查詢Contacts.CONTENT_STREQUENT_URI獲得最近聯繫人。注意這裏獲得的不是歷史通話記錄,而是系統根據通話頻率自動得到的。ui
public static void loadStrequent() { ArrayList<Contact> StrequentContacts = new ArrayList<Contact>(); 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.讀取合併後的通話記錄。spa
查詢Calls.CONTENT_URI,並不能讀取出具體的聯繫人信息,若是要知道最近通話記錄,要跟已經聯繫人列表對照使用。orm
public static void loadCallLogsCombined() { if (AllContacts.size() == 0) { loadContacts(); } ArrayList<Contact> recentContacts = new ArrayList<Contact>(); 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<Contact> 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<Contact> iterator = recentContacts.iterator(); iterator .hasNext();) { Contact con = iterator.next(); if (con.Last_Contact_Number.equals(number)) { matched = true; con.Times_Contacted++; break; } } if (!matched) { match2: for (Iterator<Contact> iterator = AllContacts .iterator(); iterator.hasNext();) { Contact con = iterator.next(); ArrayList<PhoneStruct> phones = con.getPhones(); for (Iterator<PhoneStruct> iterator2 = phones .iterator(); iterator2.hasNext();) { PhoneStruct phoneStruct = iterator2.next(); if (phoneStruct.phoneNumber.equals(number)) { matched = true; Contact tmpContact = con.clone(); tmpContact .setPhones(new ArrayList<Contact.PhoneStruct>()); 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 match2; } } } } } } cursor.close(); }