Android聯(lián)系人的一些讀取操作

源碼來自我的一個撥號應(yīng)用:https://github.com/NashLegend/QuicKid

目前成都創(chuàng)新互聯(lián)已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、太平網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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是一個自定義類,這里沒有寫出來,這不是重點(diǎn)……)

    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.讀取最近聯(lián)系人

    Android可以通過查詢Contacts.CONTENT_STREQUENT_URI得到最近聯(lián)系人。注意這里得到的不是歷史通話記錄,而是系統(tǒng)根據(jù)通話頻率自動獲得的。

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();
	// 顯示最近聯(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();
        }
        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) {
                    match3: 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 match3;
                            }
                        }
                    }
                }
            }
        }
        cursor.close();
    }

文章名稱:Android聯(lián)系人的一些讀取操作
標(biāo)題路徑:http://muchs.cn/article22/jcpocc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、、全網(wǎng)營銷推廣、品牌網(wǎng)站制作虛擬主機(jī)、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司