如何获取联系人所有字段

2024-12-03 17:37:31
推荐回答(2个)
回答1:

package cn.ihope.mozyhome.util;
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.Data;
import android.util.Log;
import cn.ihope.mozyhome.domain.Contacts;
public class GetContactsInfo {
private List list;
private Context context;
private JSONObject contactData;
private JSONObject jsonObject;
public GetContactsInfo(Context context) {
  this.context = context;
}
public String getContactInfo() throws JSONException {
  // 获得通讯录信息 ,URI是ContactsContract.Contacts.CONTENT_URI
  list = new ArrayList();
  contactData = new JSONObject();
  String mimetype = "";
  int oldrid = -1;
  int contactId = -1;
  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类型
   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 (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();
}
}

回答2:

select * from 联系人表