android如何获取联系人所有信息
只要是开发和手机通讯录有关的应用,总要学会获取联系人信息,每次都google很麻烦,怎么办?
写一个工具类,获取到通讯录里所有的信息并分好类,至于大家怎么用就不管了,看下代码就都明白了,虽然代码很多,但是很简单,大部分都已分类,如果有没有写上的,大家可以打开自己手机上通讯录数据库,里面的字段都有标明,用的内容提供者,因此我们只需要拿到那个字段名基本上就能取出数据了。
工具类:
packagecom.example.test; importjava.util.ArrayList; importjava.util.List; importorg.json.JSONException; importorg.json.JSONObject; importandroid.content.Context; importandroid.database.Cursor; importandroid.provider.ContactsContract.CommonDataKinds.Email; importandroid.provider.ContactsContract.CommonDataKinds.Event; importandroid.provider.ContactsContract.CommonDataKinds.Im; importandroid.provider.ContactsContract.CommonDataKinds.Nickname; importandroid.provider.ContactsContract.CommonDataKinds.Note; importandroid.provider.ContactsContract.CommonDataKinds.Organization; importandroid.provider.ContactsContract.CommonDataKinds.Phone; importandroid.provider.ContactsContract.CommonDataKinds.StructuredName; importandroid.provider.ContactsContract.CommonDataKinds.StructuredPostal; importandroid.provider.ContactsContract.CommonDataKinds.Website; importandroid.provider.ContactsContract.Contacts; importandroid.provider.ContactsContract.Data; importandroid.util.Log; /** * *@authorlarson * */ publicclassContactUtil{ privateListlist; privateContextcontext; privateJSONObjectcontactData; privateJSONObjectjsonObject; publicContactUtil(Contextcontext){ this.context=context; } //ContactsContract.Contacts.CONTENT_URI=content://com.android.contacts/contacts; //ContactsContract.Data.CONTENT_URI=content://com.android.contacts/data; /** *获取联系人信息,并把数据转换成json数据 * *@return *@throwsJSONException */ publicStringgetContactInfo()throwsJSONException{ list=newArrayList (); contactData=newJSONObject(); Stringmimetype=""; intoldrid=-1; intcontactId=-1; //1.查询通讯录所有联系人信息,通过id排序,我们看下android联系人的表就知道,所有的联系人的数据是由RAW_CONTACT_ID来索引开的 //所以,先获取所有的人的RAW_CONTACT_ID Cursorcursor=context.getContentResolver().query(Data.CONTENT_URI, null,null,null,Data.RAW_CONTACT_ID); intnumm=0; while(cursor.moveToNext()){ contactId=cursor.getInt(cursor .getColumnIndex(Data.RAW_CONTACT_ID)); if(oldrid!=contactId){ jsonObject=newJSONObject(); 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)); Stringprefix=cursor.getString(cursor .getColumnIndex(StructuredName.PREFIX)); jsonObject.put("prefix",prefix); StringfirstName=cursor.getString(cursor .getColumnIndex(StructuredName.FAMILY_NAME)); jsonObject.put("firstName",firstName); StringmiddleName=cursor.getString(cursor .getColumnIndex(StructuredName.MIDDLE_NAME)); jsonObject.put("middleName",middleName); Stringlastname=cursor.getString(cursor .getColumnIndex(StructuredName.GIVEN_NAME)); jsonObject.put("lastname",lastname); Stringsuffix=cursor.getString(cursor .getColumnIndex(StructuredName.SUFFIX)); jsonObject.put("suffix",suffix); StringphoneticFirstName=cursor.getString(cursor .getColumnIndex(StructuredName.PHONETIC_FAMILY_NAME)); jsonObject.put("phoneticFirstName",phoneticFirstName); StringphoneticMiddleName=cursor.getString(cursor .getColumnIndex(StructuredName.PHONETIC_MIDDLE_NAME)); jsonObject.put("phoneticMiddleName",phoneticMiddleName); StringphoneticLastName=cursor.getString(cursor .getColumnIndex(StructuredName.PHONETIC_GIVEN_NAME)); jsonObject.put("phoneticLastName",phoneticLastName); } //1.2获取各种电话信息 if(Phone.CONTENT_ITEM_TYPE.equals(mimetype)){ intphoneType=cursor .getInt(cursor.getColumnIndex(Phone.TYPE));//手机 if(phoneType==Phone.TYPE_MOBILE){ Stringmobile=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("mobile",mobile); } //住宅电话 if(phoneType==Phone.TYPE_HOME){ StringhomeNum=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("homeNum",homeNum); } //单位电话 if(phoneType==Phone.TYPE_WORK){ StringjobNum=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("jobNum",jobNum); } //单位传真 if(phoneType==Phone.TYPE_FAX_WORK){ StringworkFax=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("workFax",workFax); } //住宅传真 if(phoneType==Phone.TYPE_FAX_HOME){ StringhomeFax=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("homeFax",homeFax); }//寻呼机 if(phoneType==Phone.TYPE_PAGER){ Stringpager=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("pager",pager); } //回拨号码 if(phoneType==Phone.TYPE_CALLBACK){ StringquickNum=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("quickNum",quickNum); } //公司总机 if(phoneType==Phone.TYPE_COMPANY_MAIN){ StringjobTel=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("jobTel",jobTel); } //车载电话 if(phoneType==Phone.TYPE_CAR){ StringcarNum=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("carNum",carNum); }//ISDN if(phoneType==Phone.TYPE_ISDN){ Stringisdn=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("isdn",isdn); }//总机 if(phoneType==Phone.TYPE_MAIN){ Stringtel=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("tel",tel); } //无线装置 if(phoneType==Phone.TYPE_RADIO){ StringwirelessDev=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("wirelessDev",wirelessDev); }//电报 if(phoneType==Phone.TYPE_TELEX){ Stringtelegram=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("telegram",telegram); } //TTY_TDD if(phoneType==Phone.TYPE_TTY_TDD){ Stringtty_tdd=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("tty_tdd",tty_tdd); } //单位手机 if(phoneType==Phone.TYPE_WORK_MOBILE){ StringjobMobile=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("jobMobile",jobMobile); } //单位寻呼机 if(phoneType==Phone.TYPE_WORK_PAGER){ StringjobPager=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("jobPager",jobPager); }//助理 if(phoneType==Phone.TYPE_ASSISTANT){ StringassistantNum=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("assistantNum",assistantNum); }//彩信 if(phoneType==Phone.TYPE_MMS){ Stringmms=cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); jsonObject.put("mms",mms); } StringmobileEmail=cursor.getString(cursor .getColumnIndex(Email.DATA)); jsonObject.put("mobileEmail",mobileEmail); } } //查找event地址 if(Event.CONTENT_ITEM_TYPE.equals(mimetype)){//取出时间类型 inteventType=cursor.getInt(cursor.getColumnIndex(Event.TYPE));//生日 if(eventType==Event.TYPE_BIRTHDAY){ Stringbirthday=cursor.getString(cursor .getColumnIndex(Event.START_DATE)); jsonObject.put("birthday",birthday); } //周年纪念日 if(eventType==Event.TYPE_ANNIVERSARY){ Stringanniversary=cursor.getString(cursor .getColumnIndex(Event.START_DATE)); jsonObject.put("anniversary",anniversary); } } //获取即时通讯消息 if(Im.CONTENT_ITEM_TYPE.equals(mimetype)){//取出即时消息类型 intprotocal=cursor.getInt(cursor.getColumnIndex(Im.PROTOCOL)); if(Im.TYPE_CUSTOM==protocal){ StringworkMsg=cursor.getString(cursor .getColumnIndex(Im.DATA)); jsonObject.put("workMsg",workMsg); }elseif(Im.PROTOCOL_MSN==protocal){ StringworkMsg=cursor.getString(cursor .getColumnIndex(Im.DATA)); jsonObject.put("workMsg",workMsg); } if(Im.PROTOCOL_QQ==protocal){ StringinstantsMsg=cursor.getString(cursor .getColumnIndex(Im.DATA)); jsonObject.put("instantsMsg",instantsMsg); } } //获取备注信息 if(Note.CONTENT_ITEM_TYPE.equals(mimetype)){ Stringremark=cursor.getString(cursor.getColumnIndex(Note.NOTE)); jsonObject.put("remark",remark); } //获取昵称信息 if(Nickname.CONTENT_ITEM_TYPE.equals(mimetype)){ StringnickName=cursor.getString(cursor .getColumnIndex(Nickname.NAME)); jsonObject.put("nickName",nickName); } //获取组织信息 if(Organization.CONTENT_ITEM_TYPE.equals(mimetype)){//取出组织类型 intorgType=cursor.getInt(cursor .getColumnIndex(Organization.TYPE));//单位 if(orgType==Organization.TYPE_CUSTOM){//if(orgType== //Organization.TYPE_WORK) //{ Stringcompany=cursor.getString(cursor .getColumnIndex(Organization.COMPANY)); jsonObject.put("company",company); StringjobTitle=cursor.getString(cursor .getColumnIndex(Organization.TITLE)); jsonObject.put("jobTitle",jobTitle); Stringdepartment=cursor.getString(cursor .getColumnIndex(Organization.DEPARTMENT)); jsonObject.put("department",department); } } //获取网站信息 if(Website.CONTENT_ITEM_TYPE.equals(mimetype)){//取出组织类型 intwebType=cursor.getInt(cursor.getColumnIndex(Website.TYPE));//主页 if(webType==Website.TYPE_CUSTOM){ Stringhome=cursor.getString(cursor .getColumnIndex(Website.URL)); jsonObject.put("home",home); }//主页 elseif(webType==Website.TYPE_HOME){ Stringhome=cursor.getString(cursor .getColumnIndex(Website.URL)); jsonObject.put("home",home); } //个人主页 if(webType==Website.TYPE_HOMEPAGE){ StringhomePage=cursor.getString(cursor .getColumnIndex(Website.URL)); jsonObject.put("homePage",homePage); } //工作主页 if(webType==Website.TYPE_WORK){ StringworkPage=cursor.getString(cursor .getColumnIndex(Website.URL)); jsonObject.put("workPage",workPage); } } //查找通讯地址 if(StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)){//取出邮件类型 intpostalType=cursor.getInt(cursor .getColumnIndex(StructuredPostal.TYPE));//单位通讯地址 if(postalType==StructuredPostal.TYPE_WORK){ Stringstreet=cursor.getString(cursor .getColumnIndex(StructuredPostal.STREET)); jsonObject.put("street",street); Stringciry=cursor.getString(cursor .getColumnIndex(StructuredPostal.CITY)); jsonObject.put("ciry",ciry); Stringbox=cursor.getString(cursor .getColumnIndex(StructuredPostal.POBOX)); jsonObject.put("box",box); Stringarea=cursor.getString(cursor .getColumnIndex(StructuredPostal.NEIGHBORHOOD)); jsonObject.put("area",area); Stringstate=cursor.getString(cursor .getColumnIndex(StructuredPostal.REGION)); jsonObject.put("state",state); Stringzip=cursor.getString(cursor .getColumnIndex(StructuredPostal.POSTCODE)); jsonObject.put("zip",zip); Stringcountry=cursor.getString(cursor .getColumnIndex(StructuredPostal.COUNTRY)); jsonObject.put("country",country); } //住宅通讯地址 if(postalType==StructuredPostal.TYPE_HOME){ StringhomeStreet=cursor.getString(cursor .getColumnIndex(StructuredPostal.STREET)); jsonObject.put("homeStreet",homeStreet); StringhomeCity=cursor.getString(cursor .getColumnIndex(StructuredPostal.CITY)); jsonObject.put("homeCity",homeCity); StringhomeBox=cursor.getString(cursor .getColumnIndex(StructuredPostal.POBOX)); jsonObject.put("homeBox",homeBox); StringhomeArea=cursor.getString(cursor .getColumnIndex(StructuredPostal.NEIGHBORHOOD)); jsonObject.put("homeArea",homeArea); StringhomeState=cursor.getString(cursor .getColumnIndex(StructuredPostal.REGION)); jsonObject.put("homeState",homeState); StringhomeZip=cursor.getString(cursor .getColumnIndex(StructuredPostal.POSTCODE)); jsonObject.put("homeZip",homeZip); StringhomeCountry=cursor.getString(cursor .getColumnIndex(StructuredPostal.COUNTRY)); jsonObject.put("homeCountry",homeCountry); } //其他通讯地址 if(postalType==StructuredPostal.TYPE_OTHER){ StringotherStreet=cursor.getString(cursor .getColumnIndex(StructuredPostal.STREET)); jsonObject.put("otherStreet",otherStreet); StringotherCity=cursor.getString(cursor .getColumnIndex(StructuredPostal.CITY)); jsonObject.put("otherCity",otherCity); StringotherBox=cursor.getString(cursor .getColumnIndex(StructuredPostal.POBOX)); jsonObject.put("otherBox",otherBox); StringotherArea=cursor.getString(cursor .getColumnIndex(StructuredPostal.NEIGHBORHOOD)); jsonObject.put("otherArea",otherArea); StringotherState=cursor.getString(cursor .getColumnIndex(StructuredPostal.REGION)); jsonObject.put("otherState",otherState); StringotherZip=cursor.getString(cursor .getColumnIndex(StructuredPostal.POSTCODE)); jsonObject.put("otherZip",otherZip); StringotherCountry=cursor.getString(cursor .getColumnIndex(StructuredPostal.COUNTRY)); jsonObject.put("otherCountry",otherCountry); } } cursor.close(); Log.i("contactData",contactData.toString()); returncontactData.toString(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。