如何获取所有联系人及其所有属性

编程入门 行业动态 更新时间:2024-10-25 22:36:55
本文介绍了如何获取所有联系人及其所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人知道一种舒适的方法来获取保存在android设备上的所有联系人及其所有属性(姓名,电话,电子邮件等)吗?

does anybody know a comfortable way for getting all contacts and all of their attributes (names, phone, email, ...) that are saved on a android device?

我需要一个可以在Android 1.5到4.0上运行的解决方案。

I would need a solution that works from Android 1.5 to 4.0.

很多事

推荐答案

要获取名称和号码,请使用以下代码::

to get name and number use this code::

private void getNameNumber(){ Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; Cursor names = getContentResolver().query(uri, projection, null, null, null); int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); names.moveToFirst(); do { String name = names.getString(indexName); Log.e("Name new:", name); String number = names.getString(indexNumber); Log.e("Number new:","::"+number); } while (names.moveToNext()); }

要获取所有详细信息,请使用以下代码并根据您的要求进行修改

and to get all the details use the below code and modify it according to your requirement.

private void getDetails(){ Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER ,ContactsContract.CommonDataKinds.Email.DATA }; Cursor names = getContentResolver().query(uri, projection, null, null, null); int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); names.moveToFirst(); do { String name = names.getString(indexName); Log.e("Name new:", name); String number = names.getString(indexNumber); Log.e("Number new:","::"+number); } while (names.moveToNext()); // email while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); Cursor email = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null); while (email.moveToNext()) { //to get the contact names // if the email addresses were stored in an array String emailid = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); Log.e("Email id ::", emailid); String emailType = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); Log.e("Email Type ::", emailType); } email.close(); } //address while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID+ " = ?", new String[] { id },null); while(addrCur.moveToNext()) { String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)); Log.e("Street ::", street); String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); Log.e("City ::", city); String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)); Log.e("State ::", state); String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)); Log.e("Postal Code ::", postalCode); } addrCur.close(); } }

更多推荐

如何获取所有联系人及其所有属性

本文发布于:2023-11-24 05:24:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   联系人

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!