源碼來自我的一個撥號應(yīng)用:https://github.com/NashLegend/QuicKid
我們擁有10余年網(wǎng)頁設(shè)計和網(wǎng)站建設(shè)經(jīng)驗,從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計師為您提供的解決方案。為企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計、微信開發(fā)、成都小程序開發(fā)、成都做手機網(wǎng)站、H5技術(shù)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計或者設(shè)計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計服務(wù)并滿足您的需求。1.讀取帶電話號碼的所有聯(lián)系人。
Android系統(tǒng)貌似沒有直接取得帶電話號碼的聯(lián)系人列表的功能。直接讀取Contacts.CONTENT_URI只能讀取聯(lián)系人信息卻得不到電話號碼。如果先讀取聯(lián)系人列表,再通過聯(lián)系人列表一個一個讀取電話號碼又非常慢,所以可以這樣讀:先從Phone.CONTENT_URI讀取出電話列表,但是有可能一個人對應(yīng)多個號碼,這時只要合并一下就可以了,根據(jù)Contacts.SORT_KEY_PRIMARY排序,同一個人的不同號碼是靠在一起的,這樣合并就變得非常容易,壞處是對于沒有電話號碼的聯(lián)系人這里是取不到的。(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.讀取最近聯(lián)系人
Android可以通過查詢Contacts.CONTENT_STREQUENT_URI得到最近聯(lián)系人。注意這里得到的不是歷史通話記錄,而是系統(tǒng)根據(jù)通話頻率自動獲得的。
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(); // 顯示最近聯(lián)系人和收藏的聯(lián)系人 Cursor cursor = resolver.query(Contacts.CONTENT_STREQUENT_URI, projection, null, null, null); // 加載最近聯(lián)系人,不包括收藏的聯(lián)系人 //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,并不能讀取出具體的聯(lián)系人信息,如果要知道最近通話記錄,要跟已經(jīng)聯(lián)系人列表對照使用。
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(); }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。