Android中联系人和通话记录详解(联系人的增删改查)(3)

转自:http://blog.csdn.net/wangjiang_qianmo/article/details/49002149   

在上一章 Android中联系人和通话记录详解(2)中分析了联系人相关的表和字段,在这一章中将分析联系人相关的基本数据操作(Insert,Query,Update,Delete)。

   1.添加(Insert)

     从contacts,data,mimetypes,raw_contacts表的关系可以看出,表raw_contacts为表data的父表,所以插入数据时,应该先在raw_contacts表中插入数据,再在data表中插入数据。

[java]  view plain  copy
  1. ArrayList operations = new ArrayList();  
  2.         ContentProviderOperation op1 = ContentProviderOperation  
  3.                 .newInsert(ContactsContract.RawContacts.CONTENT_URI)  
  4.                 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)  
  5.                 .build();// 得到了一个添加内容的对象  
  6.         operations.add(op1);  
  7.   
  8.         ContentProviderOperation op2 = ContentProviderOperation  
  9.                 .newInsert(ContactsContract.Data.CONTENT_URI)  
  10.                 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)  
  11.                 .withValue(  
  12.                         ContactsContract.Data.MIMETYPE,  
  13.                         ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)  
  14.                 .withValue(ContactsContract.Data.DATA1, "小明").build();// 得到了一个添加内容的对象  
  15.         operations.add(op2);  
  16.   
  17.         ContentProviderOperation op3 = ContentProviderOperation  
  18.                 .newInsert(ContactsContract.Data.CONTENT_URI)  
  19.                 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)  
  20.                 .withValue(  
  21.                         ContactsContract.Data.MIMETYPE,  
  22.                         ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)  
  23.                 .withValue(ContactsContract.Data.DATA1, "1233232542")  
  24.                 .withValue(ContactsContract.Data.DATA2, "2")// data2=2即type=2,表示移动电话  
  25.                 .build();// 得到了一个添加内容的对象  
  26.         operations.add(op3);  
  27.   
  28.         ContentProviderOperation op4 = ContentProviderOperation  
  29.                 .newInsert(ContactsContract.Data.CONTENT_URI)  
  30.                 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)  
  31.                 .withValue(  
  32.                         ContactsContract.Data.MIMETYPE,  
  33.                         ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)  
  34.                 .withValue(ContactsContract.Data.DATA1, "[email protected]")  
  35.                 .withValue(ContactsContract.Data.DATA2, "2")// data2=2即type=2,表示工作邮箱  
  36.                 .build();// 得到了一个添加内容的对象  
  37.         operations.add(op4);  
  38.   
  39.         getContext().getContentResolver().applyBatch("com.android.contacts",  
  40.                 operations);// 执行批量操作  
ContentProviderOperation类是为了使批量更新、插入、删除数据更加方便而引入的,在官方文档中指出推荐使用ContentProviderOperation类的原因有:1.所有的操作都在一个事务中执行,这样可以保证数据完整性;2.由于批量操作在一个事务中执行,只需要打开和关闭一个事务,比多次打开关闭多个事务性能要好些;3.使用批量操作和多次单个操作相比,减少了应用和content provider之间的上下文切换,这样也会提升应用的性能,并且减少占用CPU的时间,当然也会减少电量的消耗。

     2.查询(Query)     

[java]  view plain  copy
  1. public void queryContacts(Context context) {  
  2.         ContentResolver cr = context.getContentResolver();  
  3.         Cursor contactCursor = cr.query(ContactsContract.Contacts.CONTENT_URI,  
  4.                 new String[] { ContactsContract.Contacts._ID }, nullnull,  
  5.                 ContactsContract.Contacts.SORT_KEY_PRIMARY);//在raw_contacts表中得到contactId  
  6.         if (contactCursor != null && contactCursor.getCount() > 0)  
  7.             while (contactCursor.moveToNext()) {  
  8.                 long contactId = contactCursor.getLong(contactCursor  
  9.                         .getColumnIndex(ContactsContract.Contacts._ID));  
  10.                 Cursor dataCursor = cr.query(ContactsContract.Data.CONTENT_URI,  
  11.                         new String[] { ContactsContract.Data.MIMETYPE,  
  12.                                 ContactsContract.Data.DATA1,  
  13.                                 ContactsContract.Data.DATA2,  
  14.                                 ContactsContract.Data.DATA15 },  
  15.                         ContactsContract.Data.RAW_CONTACT_ID + "=" + contactId,  
  16.                         nullnull);  
  17.                 if (dataCursor != null && dataCursor.getCount() > 0) {  
  18.                     while (dataCursor.moveToNext()) {  
  19.                         String mimeType = dataCursor  
  20.                                 .getString(dataCursor  
  21.                                         .getColumnIndex(ContactsContract.Data.MIMETYPE));  
  22.                         if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE  
  23.                                 .equals(mimeType)) {  
  24.                             //电话号码  
  25.                             dataCursor  
  26.                                     .getString(dataCursor  
  27.                                             .getColumnIndex(ContactsContract.Data.DATA1));  
  28.                         } else if (ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE  
  29.                                 .equals(mimeType)) {  
  30.                             //邮件  
  31.                             dataCursor  
  32.                                     .getString(dataCursor  
  33.                                             .getColumnIndex(ContactsContract.Data.DATA1));  
  34.                         } else if (ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE  
  35.                                 .equals(mimeType)) {  
  36.                             //即时消息  
  37.                         } else if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE  
  38.                                 .equals(mimeType)) {  
  39.                             //名字  
  40.                             dataCursor  
  41.                                     .getString(dataCursor  
  42.                                             .getColumnIndex(ContactsContract.Data.DATA1));  
  43.                         } else if (ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE  
  44.                                 .equals(mimeType)) {  
  45.                             //头像  
  46.                             byte[] data = dataCursor  
  47.                                     .getBlob(dataCursor  
  48.                                             .getColumnIndex(ContactsContract.Data.DATA15));  
  49.                             BitmapFactory.decodeByteArray(data, 0, data.length);  
  50.                         } else if (ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE  
  51.                                 .equals(mimeType)) {  
  52.                             //昵称  
  53.                         }  
  54.                     }  
  55.                 }  
  56.                 if (dataCursor != null) {  
  57.                     dataCursor.close();  
  58.                     dataCursor = null;  
  59.                 }  
  60.             }  
  61.   
  62.         if (contactCursor != null) {  
  63.             contactCursor.close();  
  64.             contactCursor = null;  
  65.         }  
  66.   
  67.     }  

    3.更新(Update)

[java]  view plain  copy
  1. ArrayList ops = new ArrayList();  
  2.         ContentProviderOperation op1 = ContentProviderOperation  
  3.                 .newUpdate(ContactsContract.Data.CONTENT_URI)  
  4.                 .withSelection(  
  5.                         ContactsContract.Data.RAW_CONTACT_ID + "=? and "  
  6.                                 + ContactsContract.Data.MIMETYPE + "=?",  
  7.                         new String[] {  
  8.                                 String.valueOf(contactId),  
  9.                                 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE })  
  10.                 .withValue(  
  11.                         ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,// 对应data表中的data1字段  
  12.                         "小明").build();  
  13.         ops.add(op1);  
  14.   
  15.         ContentProviderOperation op2 = ContentProviderOperation  
  16.                 .newUpdate(ContactsContract.Data.CONTENT_URI)  
  17.                 .withSelection(  
  18.                         ContactsContract.Data.RAW_CONTACT_ID + "=? and "  
  19.                                 + ContactsContract.Data.MIMETYPE + "=?",  
  20.                         new String[] { String.valueOf(contactId),  
  21.                                 ContactsContract.CommonDataKinds.Phone.MIMETYPE })  
  22.                 .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,// 对应data表中的data1字段  
  23.                         "12367712322").build();  
  24.         ops.add(op2);  
  25.   
  26.         ContentProviderOperation op3 = ContentProviderOperation  
  27.                 .newUpdate(ContactsContract.Data.CONTENT_URI)  
  28.                 .withSelection(  
  29.                         ContactsContract.Data.RAW_CONTACT_ID + "=? and "  
  30.                                 + ContactsContract.Data.MIMETYPE + "=?",  
  31.                         new String[] { String.valueOf(contactId),  
  32.                                 ContactsContract.CommonDataKinds.Phone.MIMETYPE })  
  33.                 .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,// 对应data表中的data1字段  
  34.                         "[email protected]").build();  
  35.         ops.add(op3);  
  36.   
  37.         try {  
  38.             context.getContentResolver().applyBatch(ContactsContract.AUTHORITY,  
  39.                     ops);  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
     4.删除(Delete)

[java]  view plain  copy
  1. ArrayList ops = new ArrayList();  
  2.                 // 先删除子表Contacts中的数据  
  3.                 ops.add(ContentProviderOperation  
  4.                         .newDelete(ContactsContract.Contacts.CONTENT_URI)  
  5.                         .withSelection(  
  6.                                 ContactsContract.Contacts._ID + "=?",  
  7.                                 new String[] { String.valueOf(mContactId) })  
  8.                         .build());  
  9.                 // 然后删除子表Data中的数据  
  10.                 ops.add(ContentProviderOperation  
  11.                         .newDelete(ContactsContract.Data.CONTENT_URI)  
  12.                         .withSelection(  
  13.                                 ContactsContract.Data.RAW_CONTACT_ID + "=?",  
  14.                                 new String[] { String.valueOf(mContactId) })  
  15.                         .build());  
  16.                 // 最后删除父表RawContacts中的数据  
  17.                 ops.add(ContentProviderOperation  
  18.                         .newDelete(ContactsContract.RawContacts.CONTENT_URI)  
  19.                         .withSelection(  
  20.                                 ContactsContract.RawContacts.CONTACT_ID  
  21.                                         + "=?",  
  22.                                 new String[] { String.valueOf(mContactId) })  
  23.                         .build());  
  24.                 try {  
  25.                     getContentResolver().applyBatch(  
  26.                             ContactsContract.AUTHORITY, ops);  
  27.                 } catch (Exception e) {  
  28.                     e.printStackTrace();  
  29.                 }  

    5.根据联系人电话号码查询名字,头像等等。

[java]  view plain  copy
  1. //根据联系人电话号码查询名字  
  2.     public static String queryNameFromContactsByNumber(Context context,  
  3.             String number) {  
  4.         String name = null;  
  5.         if (context != null && number != null && !"".equals(number.trim())) {  
  6.             Uri uri = Uri.withAppendedPath(  
  7.                     ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,  
  8.                     number);  
  9.             Cursor nameCursor = context.getContentResolver().query(uri,  
  10.                     new String[] { "display_name" }, nullnullnull);  
  11.             if (nameCursor != null) {  
  12.                 if (nameCursor.getCount() > 0) {  
  13.                     nameCursor.moveToFirst();  
  14.                     name = nameCursor.getString(0);  
  15.                 }  
  16.             }  
  17.         }  
  18.         return name;  
  19.     }  
[java]  view plain  copy
  1. //根据联系人电话查询头像  
  2. public Bitmap queryPhotoFromContactsByNumber(Context context, String number) {  
  3.         Bitmap bitmap = null;  
  4.         if (context != null && number != null && !"".equals(number.trim())) {  
  5.             Uri numberUri = Uri.withAppendedPath(  
  6.                     ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,  
  7.                     number);  
  8.             ContentResolver cr = context.getContentResolver();  
  9.             Cursor numberCursor = cr.query(numberUri,  
  10.                     new String[] { "contact_id" }, nullnullnull);  
  11.             if (numberCursor != null) {  
  12.                 if (numberCursor.getCount() > 0) {  
  13.                     numberCursor.moveToFirst();  
  14.                     long mContactId = numberCursor.getLong(0);  
  15.                     Uri contactUri = ContentUris.withAppendedId(  
  16.                             ContactsContract.Contacts.CONTENT_URI, mContactId);  
  17.                     InputStream is = ContactsContract.Contacts  
  18.                             .openContactPhotoInputStream(cr, contactUri);  
  19.                     if (is != null) {  
  20.                         bitmap = BitmapFactory.decodeStream(is);  
  21.                         numberCursor.close();  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  
  26.         return bitmap;  
  27.       

    6.通过raw_contacts的字段version,监听联系人的变化

[java]  view plain  copy
  1. /** 
  2.      * 
     
  3.      *  Description:  在raw_contacts表中获得联系人的version,如果联系人有更改,则version会变化 
  4.      *  
  5.      * @return int 联系人的version 
  6.      */  
  7.     public static int getContactVersion(Context context, long contactId) {  
  8.         Cursor cursor = context.getContentResolver().query(  
  9.                 ContactsContract.RawContacts.CONTENT_URI,  
  10.                 new String[] { ContactsContract.RawContacts.CONTACT_ID,  
  11.                         ContactsContract.RawContacts.VERSION },  
  12.                 ContactsContract.RawContacts.CONTACT_ID + "=?",  
  13.                 new String[] { String.valueOf(contactId) }, null);  
  14.         int version = 0;  
  15.         if (cursor != null && cursor.getCount() > 0) {  
  16.             cursor.moveToFirst();  
  17.             version = cursor.getInt(cursor  
  18.                     .getColumnIndex(ContactsContract.RawContacts.VERSION));  
  19.         }  
  20.         if (cursor != null) {  
  21.             cursor.close();  
  22.             cursor = null;  
  23.         }  
  24.         return version;  
  25.     }  

      到这儿联系人的Insert,Query,Update,Delete四大基本数据操作已经完成。

你可能感兴趣的:(android)