
只要是开发和手机通讯录有关的应用,总要学会获取联系人信息,每次都Google很麻烦,怎么办?
写一个工具类,获取到通讯录里所有的信息并分好类,至于大家怎么用就不管了,看下代码就都明白了,虽然代码很多,但是很简单,大部分都已分类,如果有没有写上的,大家可以打开自己手机上通讯录数据库,里面的字段都有标明,用的内容提供者,因此我们只需要拿到那个字段名基本上就能取出数据了。
工具类:
package com.example.test;import java.util.ArrayList;import java.util.List;import org.Json.JsONException;import org.Json.JsONObject;import androID.content.Context;import androID.database.Cursor;import androID.provIDer.ContactsContract.CommonDataKinds.Email;import androID.provIDer.ContactsContract.CommonDataKinds.Event;import androID.provIDer.ContactsContract.CommonDataKinds.Im;import androID.provIDer.ContactsContract.CommonDataKinds.Nickname;import androID.provIDer.ContactsContract.CommonDataKinds.Note;import androID.provIDer.ContactsContract.CommonDataKinds.Organization;import androID.provIDer.ContactsContract.CommonDataKinds.Phone;import androID.provIDer.ContactsContract.CommonDataKinds.Structuredname;import androID.provIDer.ContactsContract.CommonDataKinds.StructuredPostal;import androID.provIDer.ContactsContract.CommonDataKinds.Website;import androID.provIDer.ContactsContract.Contacts;import androID.provIDer.ContactsContract.Data;import androID.util.Log;/** * * @author larson * */public class ContactUtil { private List<Contacts> List; private Context context; private JsONObject contactData; private JsONObject JsonObject; public ContactUtil(Context context) { this.context = context; } // ContactsContract.Contacts.CONTENT_URI= content://com.androID.contacts/contacts; // ContactsContract.Data.CONTENT_URI = content://com.androID.contacts/data; /** * 获取联系人信息,并把数据转换成Json数据 * * @return * @throws JsONException */ public String getContactInfo() throws JsONException { List = new ArrayList<Contacts>(); contactData = new JsONObject(); String mimetype = ""; int oldrID = -1; int contactID = -1; // 1.查询通讯录所有联系人信息,通过ID排序,我们看下androID联系人的表就知道,所有的联系人的数据是由RAW_CONTACT_ID来索引开的 // 所以,先获取所有的人的RAW_CONTACT_ID Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI,null,Data.RAW_CONTACT_ID); int numm = 0; while (cursor.movetoNext()) { contactID = cursor.getInt(cursor .getColumnIndex(Data.RAW_CONTACT_ID)); if (oldrID != contactID) { JsonObject = new JsONObject(); contactData.put("contact" + numm,JsonObject); numm++; oldrID = contactID; } mimetype = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE)); // 取得mimetype类型,扩展的数据都在这个类型里面 // 1.1,拿到联系人的各种名字 if (Structuredname.CONTENT_ITEM_TYPE.equals(mimetype)) { cursor.getString(cursor .getColumnIndex(Structuredname.disPLAY_name)); String prefix = cursor.getString(cursor .getColumnIndex(Structuredname.PREFIX)); JsonObject.put("prefix",prefix); String firstname = cursor.getString(cursor .getColumnIndex(Structuredname.FAMILY_name)); JsonObject.put("firstname",firstname); String mIDdlename = cursor.getString(cursor .getColumnIndex(Structuredname.MIDDLE_name)); JsonObject.put("mIDdlename",mIDdlename); String lastname = cursor.getString(cursor .getColumnIndex(Structuredname.GIVEN_name)); JsonObject.put("lastname",lastname); String suffix = cursor.getString(cursor .getColumnIndex(Structuredname.SUFFIX)); JsonObject.put("suffix",suffix); String phoneticFirstname = cursor.getString(cursor .getColumnIndex(Structuredname.PHONETIC_FAMILY_name)); JsonObject.put("phoneticFirstname",phoneticFirstname); String phoneticMIDdlename = cursor.getString(cursor .getColumnIndex(Structuredname.PHONETIC_MIDDLE_name)); JsonObject.put("phoneticMIDdlename",phoneticMIDdlename); String phoneticLastname = cursor.getString(cursor .getColumnIndex(Structuredname.PHONETIC_GIVEN_name)); JsonObject.put("phoneticLastname",phoneticLastname); } // 1.2 获取各种电话信息 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { int phoneType = cursor .getInt(cursor.getColumnIndex(Phone.TYPE)); // 手机 if (phoneType == Phone.TYPE_MOBILE) { String mobile = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("mobile",mobile); } // 住宅电话 if (phoneType == Phone.TYPE_HOME) { String homeNum = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("homeNum",homeNum); } // 单位电话 if (phoneType == Phone.TYPE_WORK) { String jobNum = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("jobNum",jobNum); } // 单位传真 if (phoneType == Phone.TYPE_FAX_WORK) { String workFax = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("workFax",workFax); } // 住宅传真 if (phoneType == Phone.TYPE_FAX_HOME) { String homeFax = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("homeFax",homeFax); } // 寻呼机 if (phoneType == Phone.TYPE_PAGER) { String pager = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("pager",pager); } // 回拨号码 if (phoneType == Phone.TYPE_CALLBACK) { String quickNum = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("quickNum",quickNum); } // 公司总机 if (phoneType == Phone.TYPE_COMPANY_MAIN) { String jobTel = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("jobTel",jobTel); } // 车载电话 if (phoneType == Phone.TYPE_CAR) { String carNum = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("carNum",carNum); } // ISDN if (phoneType == Phone.TYPE_ISDN) { String isdn = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("isdn",isdn); } // 总机 if (phoneType == Phone.TYPE_MAIN) { String tel = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("tel",tel); } // 无线装置 if (phoneType == Phone.TYPE_RAdio) { String wirelessDev = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("wirelessDev",wirelessDev); } // 电报 if (phoneType == Phone.TYPE_TELEX) { String telegram = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("telegram",telegram); } // TTY_TDD if (phoneType == Phone.TYPE_TTY_TDD) { String tty_tdd = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("tty_tdd",tty_tdd); } // 单位手机 if (phoneType == Phone.TYPE_WORK_MOBILE) { String jobMobile = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("jobMobile",jobMobile); } // 单位寻呼机 if (phoneType == Phone.TYPE_WORK_PAGER) { String jobPager = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("jobPager",jobPager); } // 助理 if (phoneType == Phone.TYPE_ASSISTANT) { String assistantNum = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("assistantNum",assistantNum); } // 彩信 if (phoneType == Phone.TYPE_MMS) { String mms = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); JsonObject.put("mms",mms); } String mobileEmail = cursor.getString(cursor .getColumnIndex(Email.DATA)); JsonObject.put("mobileEmail",mobileEmail); } } // 查找event地址 if (Event.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出时间类型 int eventType = cursor.getInt(cursor.getColumnIndex(Event.TYPE)); // 生日 if (eventType == Event.TYPE_BIRTHDAY) { String birthday = cursor.getString(cursor .getColumnIndex(Event.START_DATE)); JsonObject.put("birthday",birthday); } // 周年纪念日 if (eventType == Event.TYPE_ANNIVERSARY) { String anniversary = cursor.getString(cursor .getColumnIndex(Event.START_DATE)); JsonObject.put("anniversary",anniversary); } } // 获取即时通讯消息 if (Im.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出即时消息类型 int protocal = cursor.getInt(cursor.getColumnIndex(Im.PROTOCol)); if (Im.TYPE_CUSTOM == protocal) { String workMsg = cursor.getString(cursor .getColumnIndex(Im.DATA)); JsonObject.put("workMsg",workMsg); } else if (Im.PROTOCol_MSN == protocal) { String workMsg = cursor.getString(cursor .getColumnIndex(Im.DATA)); JsonObject.put("workMsg",workMsg); } if (Im.PROTOCol_QQ == protocal) { String instantsMsg = cursor.getString(cursor .getColumnIndex(Im.DATA)); JsonObject.put("instantsMsg",instantsMsg); } } // 获取备注信息 if (Note.CONTENT_ITEM_TYPE.equals(mimetype)) { String remark = cursor.getString(cursor.getColumnIndex(Note.NOTE)); JsonObject.put("remark",remark); } // 获取昵称信息 if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) { String nickname = cursor.getString(cursor .getColumnIndex(Nickname.name)); JsonObject.put("nickname",nickname); } // 获取组织信息 if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出组织类型 int orgType = cursor.getInt(cursor .getColumnIndex(Organization.TYPE)); // 单位 if (orgType == Organization.TYPE_CUSTOM) { // if (orgType == // Organization.TYPE_WORK) // { String company = cursor.getString(cursor .getColumnIndex(Organization.COMPANY)); JsonObject.put("company",company); String jobTitle = cursor.getString(cursor .getColumnIndex(Organization.Title)); JsonObject.put("jobTitle",jobTitle); String department = cursor.getString(cursor .getColumnIndex(Organization.DEPARTMENT)); JsonObject.put("department",department); } } // 获取网站信息 if (Website.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出组织类型 int webType = cursor.getInt(cursor.getColumnIndex(Website.TYPE)); // 主页 if (webType == Website.TYPE_CUSTOM) { String home = cursor.getString(cursor .getColumnIndex(Website.URL)); JsonObject.put("home",home); } // 主页 else if (webType == Website.TYPE_HOME) { String home = cursor.getString(cursor .getColumnIndex(Website.URL)); JsonObject.put("home",home); } // 个人主页 if (webType == Website.TYPE_HOMEPAGE) { String homePage = cursor.getString(cursor .getColumnIndex(Website.URL)); JsonObject.put("homePage",homePage); } // 工作主页 if (webType == Website.TYPE_WORK) { String workPage = cursor.getString(cursor .getColumnIndex(Website.URL)); JsonObject.put("workPage",workPage); } } // 查找通讯地址 if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出邮件类型 int postalType = cursor.getInt(cursor .getColumnIndex(StructuredPostal.TYPE)); // 单位通讯地址 if (postalType == StructuredPostal.TYPE_WORK) { String street = cursor.getString(cursor .getColumnIndex(StructuredPostal.STREET)); JsonObject.put("street",street); String ciry = cursor.getString(cursor .getColumnIndex(StructuredPostal.CITY)); JsonObject.put("ciry",ciry); String Box = cursor.getString(cursor .getColumnIndex(StructuredPostal.POBox)); JsonObject.put("Box",Box); String area = cursor.getString(cursor .getColumnIndex(StructuredPostal.NEIGHBORHOOD)); JsonObject.put("area",area); String state = cursor.getString(cursor .getColumnIndex(StructuredPostal.REGION)); JsonObject.put("state",state); String zip = cursor.getString(cursor .getColumnIndex(StructuredPostal.POSTCODE)); JsonObject.put("zip",zip); String country = cursor.getString(cursor .getColumnIndex(StructuredPostal.COUNTRY)); JsonObject.put("country",country); } // 住宅通讯地址 if (postalType == StructuredPostal.TYPE_HOME) { String homeStreet = cursor.getString(cursor .getColumnIndex(StructuredPostal.STREET)); JsonObject.put("homeStreet",homeStreet); String homeCity = cursor.getString(cursor .getColumnIndex(StructuredPostal.CITY)); JsonObject.put("homeCity",homeCity); String homeBox = cursor.getString(cursor .getColumnIndex(StructuredPostal.POBox)); JsonObject.put("homeBox",homeBox); String homeArea = cursor.getString(cursor .getColumnIndex(StructuredPostal.NEIGHBORHOOD)); JsonObject.put("homeArea",homeArea); String homeState = cursor.getString(cursor .getColumnIndex(StructuredPostal.REGION)); JsonObject.put("homeState",homeState); String homeZip = cursor.getString(cursor .getColumnIndex(StructuredPostal.POSTCODE)); JsonObject.put("homeZip",homeZip); String homeCountry = cursor.getString(cursor .getColumnIndex(StructuredPostal.COUNTRY)); JsonObject.put("homeCountry",homeCountry); } // 其他通讯地址 if (postalType == StructuredPostal.TYPE_OTHER) { String otherStreet = cursor.getString(cursor .getColumnIndex(StructuredPostal.STREET)); JsonObject.put("otherStreet",otherStreet); String otherCity = cursor.getString(cursor .getColumnIndex(StructuredPostal.CITY)); JsonObject.put("otherCity",otherCity); String otherBox = cursor.getString(cursor .getColumnIndex(StructuredPostal.POBox)); JsonObject.put("otherBox",otherBox); String otherArea = cursor.getString(cursor .getColumnIndex(StructuredPostal.NEIGHBORHOOD)); JsonObject.put("otherArea",otherArea); String otherState = cursor.getString(cursor .getColumnIndex(StructuredPostal.REGION)); JsonObject.put("otherState",otherState); String otherZip = cursor.getString(cursor .getColumnIndex(StructuredPostal.POSTCODE)); JsonObject.put("otherZip",otherZip); String otherCountry = cursor.getString(cursor .getColumnIndex(StructuredPostal.COUNTRY)); JsonObject.put("otherCountry",otherCountry); } } cursor.close(); Log.i("contactData",contactData.toString()); return contactData.toString(); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android如何获取联系人所有信息全部内容,希望文章能够帮你解决android如何获取联系人所有信息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)