上篇文章《Android Contact分析(一):Data, RawContact, Contact之間的關系》已經把聯系人表之間的關系講明了,這篇文章就寫點例子,來加深一下。
一、讀取聯系人中所有姓名和電話號碼:
/**
* 查詢所有聯系人姓名及電話號碼
*/
private void readContacts(){
StringBuilder sb = new StringBuilder();
ContentResolver cr = getContentResolver();
// select * from contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
while(cursor.moveToNext()){
String id = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int iHasPhoneNum = Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
sb.append(name + " (");
if(iHasPhoneNum > 0){
Cursor numCursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
null, null);
while(numCursor.moveToNext()){
String number = numCursor.getString(
numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append(number + ")");
}
numCursor.close();
}
sb.append("\r\n");
}
cursor.close();
if(!sb.toString().isEmpty()){
Log.d(TAG, "聯系人:\r\n" + sb.toString());
}
}
1. 先從Contact中,查找所有的記錄;
2. 讀取每條Contact_ID以及姓名;
3. 根據Contact_ID去查詢該聯系人的所有電話號碼。
二、姓名模糊查詢:
/**
* 根據名字中的某一個字進行模糊查詢
* @param key
*/
private void getFuzzyQueryByName(String key){
StringBuilder sb = new StringBuilder();
ContentResolver cr = getContentResolver();
String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.Contacts.DISPLAY_NAME + " like " + "'%" + key + "%'",
null, null);
while(cursor.moveToNext()){
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append(name + " (").append(number + ")").append("\r\n");
}
cursor.close();
if(!sb.toString().isEmpty()){
Log.d(TAG, "查詢聯系人:\r\n" + sb.toString());
}
}
三、電話號碼模糊查詢:
/**
* 根據名字中的某一個字進行模糊查詢
* @param key
*/
private void getFuzzyQueryByName(String key){
StringBuilder sb = new StringBuilder();
ContentResolver cr = getContentResolver();
String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.Contacts.DISPLAY_NAME + " like " + "'%" + key + "%'",
null, null);
while(cursor.moveToNext()){
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append(name + " (").append(number + ")").append("\r\n");
}
cursor.close();
if(!sb.toString().isEmpty()){
Log.d(TAG, "查詢聯系人:\r\n" + sb.toString());
}
}
四、通過漢字返回拼音:
private void getPinyinByHanzi(String name){
ContentValues values = new ContentValues();
ContentResolver cr = getContentResolver();
Uri rawContactUri = cr.insert(
ContactsContract.RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
if(name.length() > 0){
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);
cr.insert(ContactsContract.Data.CONTENT_URI, values);
String[] projection = {"sort_key"};
String where = ContactsContract.RawContacts.CONTACT_ID + "=" + rawContactId;
Cursor cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection, where, null, null);
if(cursor != null){
cursor.moveToFirst();
String pinyin = cursor.getString(cursor.getColumnIndex("sort_key"));
Log.d(TAG, pinyin);
String res = "";
for(int i = 0; i < pinyin.length(); i ++){
String temp = pinyin.substring(i, i+1);
if(temp.matches("[a-zA-Z ]")){
res += temp;
}
}
res = res.substring(0, res.length()-1);
Log.d(TAG, name + " translate = \"" + res.toLowerCase(Locale.getDefault()) + "\"");
}
}
cr.delete(ContentUris.withAppendedId(
ContactsContract.RawContacts.CONTENT_URI, rawContactId),
null, null);
}
這段代碼,是利用了RawContact中,有個"sort_key"的字段,這個字段的內容將會是拼音+漢字,如:輸入“聯系人”,"sort_key"中的內容是“LIAN聯 XI系 REN人 ”。
思想如下:
1. 在RawContact中,添加一行,則會返回一個RawContact URI,根據這個URI,可以得到Raw Contact ID;
2. 根據Raw Contact ID,向Data中的,添加名字;
3. 根據Raw Contact ID,查詢RawContact表,取出"sort_key"字段;
4. 根據Raw Contact ID,刪除這條記錄,則在Contact, Data中都會自動刪除。
