Android聯繫人的一些讀取操作

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


1.讀取帶電話號碼的所有聯繫人。

    Android系統貌似沒有直接取得帶電話號碼的聯繫人列表的功能。直接讀取Contacts.CONTENT_URI只能讀取聯繫人信息卻得不到電話號碼。如果先讀取聯繫人列表,再通過聯繫人列表一個一個讀取電話號碼又非常慢,所以可以這樣讀:先從Phone.CONTENT_URI讀取出電話列表,但是有可能一個人對應多個號碼,這時只要合併一下就可以了,根據Contacts.SORT_KEY_PRIMARY排序,同一個人的不同號碼是靠在一起的,這樣合併就變得非常容易,壞處是對於沒有電話號碼的聯繫人這裏是取不到的。(Contact是一個自定義類,這裏沒有寫出來,這不是重點……)

    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.讀取最近聯繫人

    Android可以通過查詢Contacts.CONTENT_STREQUENT_URI得到最近聯繫人。注意這裏得到的不是歷史通話記錄,而是系統根據通話頻率自動獲得的。

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.讀取合併後的通話記錄。

    查詢Calls.CONTENT_URI,並不能讀取出具體的聯繫人信息,如果要知道最近通話記錄,要跟已經聯繫人列表對照使用。

    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();
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章