现在的位置: 首页 > 综合 > 正文

Android Contact分析(二):实战篇之读取联系人,模糊查询,通过汉字返回拼音

2018年04月21日 ⁄ 综合 ⁄ 共 5853字 ⁄ 字号 评论关闭

上篇文章《Android Contact分析(一):Data, RawContact, Contact之间的关系》已经把联系人表之间的关系讲明了,这篇文章就写点例子,来加深一下。

一、读取联系人中所有姓名和电话号码:

[java] view
plain
copy

  1. /** 
  2.  * 查询所有联系人姓名及电话号码 
  3.  */  
  4. private void readContacts(){  
  5.     StringBuilder sb = new StringBuilder();  
  6.     ContentResolver cr = getContentResolver();  
  7.       
  8.     // select * from contacts  
  9.     Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,   
  10.             nullnullnullnull);  
  11.     while(cursor.moveToNext()){  
  12.         String id = cursor.getString(  
  13.                 cursor.getColumnIndex(ContactsContract.Contacts._ID));  
  14.         String name = cursor.getString(  
  15.                 cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  
  16.         int iHasPhoneNum = Integer.parseInt(cursor.getString(  
  17.                 cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));  
  18.         sb.append(name + " (");  
  19.           
  20.         if(iHasPhoneNum > 0){  
  21.             Cursor numCursor = cr.query(  
  22.                     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
  23.                     null,   
  24.                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,   
  25.                     nullnull);  
  26.             while(numCursor.moveToNext()){  
  27.                 String number = numCursor.getString(  
  28.                         numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  29.                 sb.append(number + ")");  
  30.             }  
  31.             numCursor.close();  
  32.         }  
  33.         sb.append("\r\n");  
  34.     }  
  35.     cursor.close();  
  36.       
  37.     if(!sb.toString().isEmpty()){  
  38.         Log.d(TAG, "联系人:\r\n" + sb.toString());  
  39.     }  
  40. }  

1. 先从Contact中,查找所有的记录;

2. 读取每条Contact_ID以及姓名;

3. 根据Contact_ID去查询该联系人的所有电话号码。

二、姓名模糊查询:

[java] view
plain
copy

  1. /** 
  2.  * 根据名字中的某一个字进行模糊查询 
  3.  * @param key 
  4.  */  
  5. private void getFuzzyQueryByName(String key){  
  6.       
  7.     StringBuilder sb = new StringBuilder();  
  8.     ContentResolver cr = getContentResolver();  
  9.     String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,  
  10.                 ContactsContract.CommonDataKinds.Phone.NUMBER};  
  11.     Cursor cursor = cr.query(  
  12.             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
  13.             projection,   
  14.             ContactsContract.Contacts.DISPLAY_NAME + " like " + "'%" + key + "%'",   
  15.             nullnull);  
  16.     while(cursor.moveToNext()){  
  17.         String name = cursor.getString(  
  18.                 cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  
  19.         String number = cursor.getString(  
  20.                 cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  21.         sb.append(name + " (").append(number + ")").append("\r\n");  
  22.     }  
  23.     cursor.close();  
  24.       
  25.     if(!sb.toString().isEmpty()){  
  26.         Log.d(TAG, "查询联系人:\r\n" + sb.toString());  
  27.     }  
  28. }  

三、电话号码模糊查询:

[java] view
plain
copy

  1. /** 
  2.  * 根据名字中的某一个字进行模糊查询 
  3.  * @param key 
  4.  */  
  5. private void getFuzzyQueryByName(String key){  
  6.       
  7.     StringBuilder sb = new StringBuilder();  
  8.     ContentResolver cr = getContentResolver();  
  9.     String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,  
  10.                 ContactsContract.CommonDataKinds.Phone.NUMBER};  
  11.     Cursor cursor = cr.query(  
  12.             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
  13.             projection,   
  14.             ContactsContract.Contacts.DISPLAY_NAME + " like " + "'%" + key + "%'",   
  15.             nullnull);  
  16.     while(cursor.moveToNext()){  
  17.         String name = cursor.getString(  
  18.                 cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  
  19.         String number = cursor.getString(  
  20.                 cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  21.         sb.append(name + " (").append(number + ")").append("\r\n");  
  22.     }  
  23.     cursor.close();  
  24.       
  25.     if(!sb.toString().isEmpty()){  
  26.         Log.d(TAG, "查询联系人:\r\n" + sb.toString());  
  27.     }  
  28. }  

四、通过汉字返回拼音:

[java] view
plain
copy

  1. private void getPinyinByHanzi(String name){  
  2.     ContentValues values = new ContentValues();  
  3.     ContentResolver cr = getContentResolver();  
  4.       
  5.     Uri rawContactUri = cr.insert(  
  6.             ContactsContract.RawContacts.CONTENT_URI, values);  
  7.     long rawContactId = ContentUris.parseId(rawContactUri);  
  8.       
  9.     if(name.length() > 0){  
  10.         values.clear();  
  11.         values.put(Data.RAW_CONTACT_ID, rawContactId);  
  12.         values.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);  
  13.         values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);  
  14.         cr.insert(ContactsContract.Data.CONTENT_URI, values);  
  15.           
  16.         String[] projection = {"sort_key"};  
  17.         String where = ContactsContract.RawContacts.CONTACT_ID + "=" + rawContactId;  
  18.         Cursor cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection, where, nullnull);  
  19.         if(cursor != null){  
  20.             cursor.moveToFirst();  
  21.             String pinyin = cursor.getString(cursor.getColumnIndex("sort_key"));  
  22.             Log.d(TAG, pinyin);  
  23.               
  24.             String res = "";  
  25.             for(int i = 0; i < pinyin.length(); i ++){  
  26.                 String temp = pinyin.substring(i, i+1);  
  27.                 if(temp.matches("[a-zA-Z ]")){  
  28.                     res += temp;  
  29.                 }  
  30.             }  
  31.             res = res.substring(0, res.length()-1);  
  32.             Log.d(TAG, name + " translate = \"" + res.toLowerCase(Locale.getDefault()) + "\"");  
  33.         }  
  34.     }  
  35.       
  36.     cr.delete(ContentUris.withAppendedId(  
  37.             ContactsContract.RawContacts.CONTENT_URI, rawContactId),   
  38.             nullnull);  
  39. }  

这段代码,是利用了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中都会自动删除。

抱歉!评论已关闭.