Android 获取联系人手机号码、姓名、地址、公司、邮箱、生日
public void testGetAllContact() throws Throwable
{
//获取联系人信息的Uri
Uri uri = ContactsContract.Contacts.CONTENT_URI;
//获取ContentResolver
ContentResolver contentResolver = ContactListActivity.this.getContentResolver();
//查询数据,返回Cursor
Cursor cursor = contentResolver.query(uri, null, null, null, null);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
while(cursor.moveToNext())
{
Map<String,Object> map = new HashMap<String,Object>();
StringBuilder sb = new StringBuilder();
//获取联系人的ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人的姓名
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//构造联系人信息
sb.append("contactId=").append(contactId).append(",Name=").append(name);
map.put("name", name);
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));//联系人ID //查询电话类型的数据操作
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while(phones.moveToNext())
{
String phoneNumber = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
//添加Phone的信息
sb.append(",Phone=").append(phoneNumber);
map.put("mobile", phoneNumber);
}
phones.close(); //查询Email类型的数据操作
Cursor emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
while (emails.moveToNext())
{
String emailAddress = emails.getString(emails.getColumnIndex(
ContactsContract.CommonDataKinds.Email.DATA));
//添加Email的信息
sb.append(",Email=").append(emailAddress);
Log.e("emailAddress", emailAddress);
map.put("email", emailAddress); }
emails.close();
//Log.i("=========ddddddddddd=====", sb.toString()); //查询==地址==类型的数据操作.StructuredPostal.TYPE_WORK
Cursor address = contentResolver.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
null, null);
while (address.moveToNext())
{
String workAddress = address.getString(address.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.DATA)); //添加Email的信息
sb.append(",address").append(workAddress);
map.put("address", workAddress);
}
address.close();
//Log.i("=========ddddddddddd=====", sb.toString()); //查询==公司名字==类型的数据操作.Organization.COMPANY ContactsContract.Data.CONTENT_URI
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = contentResolver.query(ContactsContract.Data.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
//组织名 (公司名字)
String company = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
//职位
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
sb.append(",company").append(company);
sb.append(",title").append(title);
map.put("company", company);
map.put("title", title);
}
orgCur.close();
list.add(map);
Log.i("=========orgName=====", sb.toString());//查看所有的数据
Log.e("=========map=====", map.toString());//有很多数据的时候,只会添加一条 例如邮箱,
} Log.i("=========list=====", list.toString());//
cursor.close();
}
权限:
<!-- 读联系人权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!-- 写联系人权限 -->
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<!-- 拨号权限 -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- 读短信权限 -->
<uses-permission android:name="android.permission.READ_SMS" />
例子:
package com.kelly.smsread;
import java.util.ArrayList;
import java.util.List; import org.json.JSONException;
import org.json.JSONObject; import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.CallLog.Calls;
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 {
public 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 = -;
int contactId = -;
// 1.查询通讯录所有联系人信息,通过id排序,我们看下android联系人的表就知道,所有的联系人的数据是由RAW_CONTACT_ID来索引开的
// 所以,先获取所有的人的RAW_CONTACT_ID
Uri uri = ContactsContract.Data.CONTENT_URI; // 联系人Uri;
Cursor cursor = context.getContentResolver().query(uri,
null, null, null, Data.RAW_CONTACT_ID);
int numm = ;
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();
} }
import cn.ihope.mozyhome.domain.Contacts; http://blog.csdn.net/yemh111/article/details/7171349
直接可以运行的 记得要加配置文件
package com.kelly.smsread; import org.json.JSONException;
import org.json.JSONObject; import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
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.Data;
import android.util.Log; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private JSONObject contactData;
private JSONObject jsonObject;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
getContactInfo();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public String getContactInfo() throws JSONException {
// 获得通讯录信息 ,URI是ContactsContract.Contacts.CONTENT_URI contactData = new JSONObject();
String mimetype = "";
int oldrid = -;
int contactId = -;
Cursor cursor = getContentResolver().query(Data.CONTENT_URI,null, null, null, Data.RAW_CONTACT_ID);
int numm=;
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类型
mimetype = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE));
// 获得通讯录中每个联系人的ID
// 获得通讯录中联系人的名字
if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
// String display_name = 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);
}
// 获取电话信息
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);
}
}
// }
// 查找email地址
if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
// 取出邮件类型
int emailType = cursor.getInt(cursor.getColumnIndex(Email.TYPE)); // 住宅邮件地址
if (emailType == Email.TYPE_CUSTOM) {
String homeEmail = cursor.getString(cursor.getColumnIndex(Email.DATA));
jsonObject.put("homeEmail", homeEmail);
} // 住宅邮件地址
else if (emailType == Email.TYPE_HOME) {
String homeEmail = cursor.getString(cursor.getColumnIndex(Email.DATA));
jsonObject.put("homeEmail", homeEmail);
}
// 单位邮件地址
if (emailType == Email.TYPE_CUSTOM) {
String jobEmail = cursor.getString(cursor.getColumnIndex(Email.DATA));
jsonObject.put("jobEmail", jobEmail);
} // 单位邮件地址
else if (emailType == Email.TYPE_WORK) {
String jobEmail = cursor.getString(cursor.getColumnIndex(Email.DATA));
jsonObject.put("jobEmail", jobEmail);
}
// 手机邮件地址
if (emailType == Email.TYPE_CUSTOM) {
String mobileEmail = cursor.getString(cursor.getColumnIndex(Email.DATA));
jsonObject.put("mobileEmail", mobileEmail);
} // 手机邮件地址
else if (emailType == Email.TYPE_MOBILE) {
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获取联系人示例,从数据库加载,带首字母标签
这几天打算学习下Android联系人方便的一些东西,之前稍有涉略,不过每次都是浅尝辄止. 推荐国内两个Link: http://fanfq.iteye.com/blog/779569 http://w ...
- Android获取本机IP地址
一.概述 习惯了Linux下的网络编程,在还没用智能机之前就一直想知道怎么得到手机的IP地址(玩智能机之前我是不搞手机应用的).好了,得知Android是基于Linux内核的,那么不就可以利用之前学的 ...
- Android -- 获取IP和MAC地址
通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法: 首先新建一个工程,修改AndroidManifest.xml文件 ...
- Android 获取联系人的号码的类型描述
...... int index = phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int t ...
- ContentProvider往通讯录添加联系人和获取联系人
public class MainActivity extends Activity { private People people; private List<People> pList ...
- android中ContentProvider获取联系人 总结
35.内容提供者:ContentResolver 用内容提供者来获取联系人信息 35-1:权限 <!-- 对联系人的读.写权限 --> <uses-permission androi ...
- 017 Android 获取手机SIM卡序列号和读取联系人
1.获取手机SIM卡序列号 //5.存储sim卡系列号 //5.1获取sim卡系列号 TelephonyManager manager = (TelephonyManager) getSystemSe ...
- Android 手机卫士--获取联系人信息并显示与回显
前面的文章已经实现相关的布局,本文接着进行相关的功能实现 本文地址:http://www.cnblogs.com/wuyudong/p/5951794.html,转载请注明出处. 读取系统联系人 当点 ...
- android 获取系统联系人 完全解析
一.代码 1.ContactsEngine.java import java.util.ArrayList; import java.util.HashMap; import java.util.Li ...
随机推荐
- wc---计算字数。
Linux wc命令用于计算字数. 利用wc指令我们可以计算文件的Byte数.字数.或是列数,若不指定文件名称.或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据. 语 ...
- 教你破解隔壁妹子wifi密码,成功率高达90%
破解wifi密码听起来很复杂,实际上也不是非常的复杂,今天教大家如何破解隔壁妹子的wifi密码. 首先声明:本教程只用于技术交流,请勿用于非法用途.请严格遵循相关法律法规.为了保护本例中被破解密码者信 ...
- [c++]基类对象作为函数參数(赋值兼容规则)
编程处理教师的基本情况. 要求: 1.定义一个"person"类.用来存储及处理人的姓名.性别.年龄,成员函数自定: 2.定义"teacher"类,公有继承&q ...
- 漫漫人生路,学点Jakarta基础-Java8函数式编程
接口默认方法 Java8版本以后新增了接口的默认方法,不仅仅只能包含抽象方法,接口也可以包含若干个实例方法.在接口内定义实例方法(但是注意需要使用default关键字) 在此定义的方法并非抽象方法,而 ...
- 如何才能正确的关闭Socket连接
从TCP协议角度来看,一个已建立的TCP连接有两种关闭方式,一种是正常关闭,即四次挥手关闭连接:还有一种则是异常关闭,我们通常称之为连接重置(RESET).首先说一下正常关闭时四次挥手的状态变迁,关闭 ...
- #学习笔记#——JavaScript 数组部分编程(六)
14. 题目描述 实现一个打点计时器,要求 1.从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 1 2.返回的对象中需 ...
- [ Java ] [ Spring ] Spring 一些配置项 及 <context:annotation-config/> 專文解释说明
節錄重點: @ Resource .@ PostConstruct.@ PreDestro.@PersistenceContext.@Required 都必須聲明相關的 bean 所以如果總是需要按照 ...
- Android中级第九讲--相机调焦
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 相机调焦:原理,使用竖直seekbar,根据用户拖拉来获得距离 ...
- 洛谷——P3178 [HAOI2015]树上操作
https://www.luogu.org/problem/show?pid=3178#sub 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 ...
- Chrome不能在网易网盘中上传文件的解决办法
Chrome不能在网易网盘中上传文件的解决办法1. 安装 Adobe Flash Player PPAPI,设置flash插件 chrome://settings/content/flash,许可[* ...