android如何獲取聯(lián)系人所有信息
只要是開發(fā)和手機(jī)通訊錄有關(guān)的應(yīng)用,總要學(xué)會獲取聯(lián)系人信息,每次都google很麻煩,怎么辦?
寫一個工具類,獲取到通訊錄里所有的信息并分好類,至于大家怎么用就不管了,看下代碼就都明白了,雖然代碼很多,但是很簡單,大部分都已分類,如果有沒有寫上的,大家可以打開自己手機(jī)上通訊錄數(shù)據(jù)庫,里面的字段都有標(biāo)明,用的內(nèi)容提供者,因此我們只需要拿到那個字段名基本上就能取出數(shù)據(jù)了。
工具類:
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;
/**
* 獲取聯(lián)系人信息,并把數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù)
*
* @return
* @throws JSONException
*/
public String getContactInfo() throws JSONException {
list = new ArrayList<Contacts>();
contactData = new JSONObject();
String mimetype = "";
int oldrid = -1;
int contactId = -1;
// 1.查詢通訊錄所有聯(lián)系人信息,通過id排序,我們看下android聯(lián)系人的表就知道,所有的聯(lián)系人的數(shù)據(jù)是由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類型,擴(kuò)展的數(shù)據(jù)都在這個類型里面
// 1.1,拿到聯(lián)系人的各種名字
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)); // 手機(jī)
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);
} // 尋呼機(jī)
if (phoneType == Phone.TYPE_PAGER) {
String pager = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("pager", pager);
}
// 回?fù)芴柎a
if (phoneType == Phone.TYPE_CALLBACK) {
String quickNum = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("quickNum", quickNum);
}
// 公司總機(jī)
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);
} // 總機(jī)
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);
}
// 單位手機(jī)
if (phoneType == Phone.TYPE_WORK_MOBILE) {
String jobMobile = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("jobMobile", jobMobile);
}
// 單位尋呼機(jī)
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);
}
// 周年紀(jì)念日
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);
}
}
// 獲取網(wǎng)站信息
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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ContentProvider實現(xiàn)手機(jī)聯(lián)系人讀取和插入
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項目
- android仿微信聯(lián)系人索引列表功能
- Android保存聯(lián)系人到通訊錄的方法
- Android使用AsyncQueryHandler實現(xiàn)獲取手機(jī)聯(lián)系人功能
- Android ContentProvider實現(xiàn)獲取手機(jī)聯(lián)系人功能
- Android 獲取手機(jī)聯(lián)系人實例代碼詳解
- android實現(xiàn)讀取、搜索聯(lián)系人的代碼
- Android ContentProvider獲取手機(jī)聯(lián)系人實例
- Android小程序?qū)崿F(xiàn)訪問聯(lián)系人
相關(guān)文章
Android編程實現(xiàn)仿iphone抖動效果的方法(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)仿iphone抖動效果的方法,結(jié)合實例形式分析了仿iphone抖動效果的頁面布局及功能實現(xiàn)技巧,并附帶實例源碼供讀者下載,需要的朋友可以參考下2015-11-11
Android中AutoCompleteTextView自動提示
這篇文章主要為大家詳細(xì)介紹了Android中AutoCompleteTextView自動提示的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié)
這篇文章主要介紹了Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié),Fragment的存在是為了解決不同屏幕分辯率的動態(tài)和靈活UI設(shè)計,需要的朋友可以參考下2016-02-02
Android編程實現(xiàn)加載等待ProgressDialog的方法
這篇文章主要介紹了Android編程實現(xiàn)加載等待ProgressDialog的方法,實例分析了Android中加載等待類ProgressDialog的具體使用方法,需要的朋友可以參考下2015-12-12
Android中Glide加載圖片并實現(xiàn)圖片緩存
本篇文章主要介紹了Android中Glide加載圖片并實現(xiàn)圖片緩存,這里和大家簡單的分享一下Glide的使用方法以及緩存 ,有興趣的可以了解一下。2017-03-03
AndroidStudio更新出現(xiàn)Refreshing ''xxx'' Gradle Project狀態(tài)解決辦法
這篇文章主要介紹了AndroidStudio更新出現(xiàn)Refreshing 'xxx' Gradle Project狀態(tài)解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Flutter實現(xiàn)底部導(dǎo)航欄創(chuàng)建詳解
ConvexBottomBar是一個底部導(dǎo)航欄組件,用于展現(xiàn)凸起的TAB效果,支持多種內(nèi)置樣式與動畫交互。本文將利用ConvexBottomBar創(chuàng)建漂亮的底部導(dǎo)航欄,感興趣的可以學(xué)習(xí)一下2022-01-01

