温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android联系人的一些读取操作

发布时间:2020-07-24 16:09:54 来源:网络 阅读:2693 作者:NashLegend 栏目:移动开发

源码来自我的一个拨号应用: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) {                     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();     }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI