android如何获取联系人所有信息
程序员文章站
2022-05-17 22:36:18
只要是开发和手机通讯录有关的应用,总要学会获取联系人信息,每次都google很麻烦,怎么办?
写一个工具类,获取到通讯录里所有的信息并分好类,至于大家怎么用就不管了,看下...
只要是开发和手机通讯录有关的应用,总要学会获取联系人信息,每次都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, null, 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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。