资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android联系人的一些读取操作

源码来自我的一个拨号应用:https://github.com/NashLegend/QuicKid

目前成都创新互联已为1000多家的企业提供了网站建设、域名、虚拟空间、网站托管、服务器托管、企业网站设计、太平网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

1.读取带电话号码的所有联系人。

    Android系统貌似没有直接取得带电话号码的联系人列表的功能。直接读取Contacts.CONTENT_URI只能读取联系人信息却得不到电话号码。如果先读取联系人列表,再通过联系人列表一个一个读取电话号码又非常慢,所以可以这样读:先从Phone.CONTENT_URI读取出电话列表,但是有可能一个人对应多个号码,这时只要合并一下就可以了,根据Contacts.SORT_KEY_PRIMARY排序,同一个人的不同号码是靠在一起的,这样合并就变得非常容易,坏处是对于没有电话号码的联系人这里是取不到的。(Contact是一个自定义类,这里没有写出来,这不是重点……)

    public static void loadContacts(Context context) {
        ArrayList AllContacts = 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() {
	ArrayList StrequentContacts = 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();
        }
        ArrayList recentContacts = 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();
    }

分享名称:Android联系人的一些读取操作
标题网址:http://cdkjz.cn/article/jcpocc.html
十二年 建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220