android 연락처 모든 정보 얻기

16532 단어 연락처소유하다
휴대전화 통신로와 관련된 앱을 개발하려면 항상 연락처 정보를 얻는 것을 배워야 한다. 매번 구글은 매우 번거롭다. 어떡하지?하나의 도구 클래스를 써서 통신록에 있는 모든 정보를 얻고 클래스를 나누면 사람들이 어떻게 사용하든 상관없다. 코드를 보면 알 수 있다. 코드가 많지만 매우 간단하다. 대부분이 이미 분류되어 있다. 만약에 작성된 것이 없으면 여러분은 자신의 핸드폰에 있는 통신록 데이터베이스를 열 수 있다. 안의 필드는 모두 표시되어 있고 사용한 내용은 제공자가 있다.그래서 우리는 그 필드 이름만 얻으면 기본적으로 데이터를 꺼낼 수 있다.
 
출처를 밝혀 주셔서 감사합니다.
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();
	}
}

좋은 웹페이지 즐겨찾기