Android PhoneBook
1.权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2.通过ContentResolver.query取得电话簿的信息
CONTENT_URI
Contacts URI:
ContactsContract.Contacts.CONTENT_URI
Phone URI:
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
Email URI:
ContactsContract.CommonDataKinds.Email.CONTENT_URI
2-1.通过取得Contacts信息
2-2.根据ContactsContract.Contacts._ID匹配ContactsContract.CommonDataKinds.Phone.CONTACT_ID来找到Phone Number等信息
2-3.根据ContactsContract.Contacts._ID匹配ContactsContract.CommonDataKinds.Email.CONTACT_ID来找到Email信息
注意:还可以更加内置的VCARD情报来取得Vcard信息。
a.取得ContactsContract.Contacts.LOOKUP_KEY
b.通过以下方式取得Vcard信息
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookkey);
AssetFileDescriptor afd = cResolver.openAssetFileDescriptor(uri, "r");
FileInputStream fis = afd.createInputStream();
byte[] buf = new byte[(int) afd.getDeclaredLength()];
3.VCard 格式:
----------------------------------------------------------
BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:100 Waters Edge=0D=0ABaytown, LA 30314=0D=0AUnited States of America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:42 Plantation St.=0D=0ABaytown, LA 30314=0D=0AUnited States of America
EMAIL;PREF;INTERNET:[email protected]
REV:20080424T195243Z
END:VCARD
----------------------------------------------------------
源代码:
Vcard.java
package com.jumper.android.demos.model; import java.util.ArrayList; import java.util.HashMap; import android.provider.ContactsContract; import android.util.Log; public class VCard { private static final String LOG_TAG = VCard.class.getName(); private static final String VCARD_BEGIN = "BEGIN:VCARD\n\n"; private static final String VCARD_VERSION = "VERSION:2.1\n\n"; private static final String VCARD_END = "END:VCARD\n\n"; public static final String TEL_TYPE = "TYPE"; public static final String TEL_NUM = "NUM"; public static final String EMAIL_TYPE = "TYPE"; public static final String EMAIL_ADDR = "ADDR"; private StringBuilder vcard = null; private ArrayList<HashMap<String, String>> mTels = null; private ArrayList<HashMap<String, String>> mEmail = null; private String mName = ""; private String mRawInfo = ""; public VCard() { vcard = new StringBuilder(); mTels = new ArrayList<HashMap<String, String>>(); mEmail = new ArrayList<HashMap<String,String>>(); vcard.append(VCARD_BEGIN); vcard.append(VCARD_VERSION); } public void setRawInfo(String cardinfo) { mRawInfo = cardinfo; } public String getRawInfo() { return mRawInfo; } public void setName(String name) { String nName = "N:" + name + "\n\n"; vcard.append(nName); String fnName = "FN:" + name + "\n\n"; vcard.append(fnName); mName = name; Log.v(LOG_TAG, "Name=" + name); } public String getName() { return mName; } public void setTel(int type, String number) { String tel = "TEL;"; switch(type) { case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: tel += "HOME:" + number; break; case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: tel += "CELL:" + number; break; case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: tel += "WORK:" + number; break; default: tel += "OTHER:" + number; break; } tel += "\n\n"; HashMap<String, String> item = new HashMap<String, String>(); item.put(TEL_TYPE, Integer.toString(type)); item.put(TEL_NUM, number); mTels.add(item); vcard.append(tel); Log.v(LOG_TAG, "tel=" + tel); } public ArrayList<HashMap<String, String>> getTels() { return mTels; } public void setEmail(int type, String address) { String email = "EMAIL;"; switch(type) { case ContactsContract.CommonDataKinds.Email.TYPE_HOME: email += "HOME:" + address; break; case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE: email += "CELL:" + address; break; case ContactsContract.CommonDataKinds.Email.TYPE_WORK: email += "WORK:" + address; break; default: email += "OTHER:" + address; break; } email += "\n\n"; HashMap<String, String> item = new HashMap<String, String>(); item.put(EMAIL_TYPE, Integer.toString(type)); item.put(EMAIL_ADDR, address); mEmail.add(item); vcard.append(email); Log.v(LOG_TAG, "email=" + email); } public ArrayList<HashMap<String, String>> getEmail() { return mEmail; } public String toString() { String ret = ""; if(mRawInfo.equals("")) { vcard.append(VCARD_END); ret = vcard.toString(); } else { ret = mRawInfo; } //Log.v(LOG_TAG, "Vcard=" + ret); return ret; } }
PhoneBook.java
package com.jumper.android.demos.call; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import com.jumper.android.demos.model.VCard; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract; public class PhoneBook { private Context mContext = null; public PhoneBook(Context context) { mContext = context; } /** * If the input parameter is null or space, return all contacts info. * @param name * @return vcard string */ public ArrayList<VCard> getPerson(String name){ ArrayList<VCard> vcardList = null; String selection = null; if(name != null && !"".equals(name.trim())) { selection = ContactsContract.Contacts.DISPLAY_NAME + " like \'%" + name + "%\'"; } ContentResolver cResolver = mContext.getContentResolver(); Cursor cursor = cResolver.query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, null); if(cursor != null) { vcardList = new ArrayList<VCard>(); while(cursor.moveToNext()) { VCard vcard = new VCard(); int colIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); String disname = cursor.getString(colIdx); vcard.setName(disname); colIdx = cursor.getColumnIndex(ContactsContract.Contacts._ID); String _id = cursor.getString(colIdx); colIdx = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); String lookkey = cursor.getString(colIdx); // Telephone Number colIdx = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); int has_phone_num = cursor.getInt(colIdx); if(has_phone_num > 0) { selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?"; Cursor telCursor = cResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection, new String[]{_id}, null); if(telCursor != null) { while(telCursor.moveToNext()) { colIdx = telCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); int type = telCursor.getInt(colIdx); colIdx = telCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String telno = telCursor.getString(colIdx); vcard.setTel(type, telno); } telCursor.close(); } } // Email Address selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?";; Cursor emailCursor = cResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, new String[]{_id}, null); if(emailCursor != null) { while(emailCursor.moveToNext()) { colIdx = emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE); int type = emailCursor.getInt(colIdx); colIdx = emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); String address = emailCursor.getString(colIdx); vcard.setEmail(type, address); } emailCursor.close(); } vcardList.add(vcard); // Get Embedded Vcard Info // Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookkey); // try { // AssetFileDescriptor afd = cResolver.openAssetFileDescriptor(uri, "r"); // FileInputStream fis = afd.createInputStream(); // byte[] buf = new byte[(int) afd.getDeclaredLength()]; // if (fis.read(buf) > 0) { // String rawinfo = new String(buf); // vcard.setRawInfo(rawinfo); // } // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } } return vcardList; } }