Android Contact分析(二):实战篇之读取联系人,模糊查询,通过汉字返回拼音
上篇文章《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中都会自动删除。