分享

Android从零开始(10)(联系人数据的读取和写入)(新)

 北斗烛龙 2014-12-25

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!


前言

手机内部其实就是用我们学过的Sqlite数据库存储我们的常用数据,如:手机联系人,手机上面的图片地址,手机铃声等等,接下来我们来学习如何读写手机中的联系人数据。

联系人数据的读取和写入

了解联系人数据的存储方式

图片1.png

以数据库(sqlite)存储主要使用到三张表

图片2.png


联系人数据的读取

  1. public void TestQuery()throws Exception{  
  2. // 得到域名(通过观察源码得知)  
  3.                         String path = ContactsContract.AUTHORITY;  
  4. // 操作raw_contacts  
  5.                         Uri uri = Uri.parse("content://" + path + "/raw_contacts");  
  6.                         ContentResolver resolver = this.getContext().getContentResolver();  
  7. //查询id 并且只查询没有删除的  
  8.                         Cursor cursor = resolver.query(uri, new String[]{"_id"}, "deleted=?"new String[]{"0"}, null);  
  9.                         while(cursor.moveToNext())  
  10.                         {  
  11. //得到ID  
  12.                                 String id = cursor.getString(0);  
  13.                                 Log.i(TAG, "This is id:" + cursor.getString(0));  
  14. //通过ID我们在到data表里面查询关于该ID相关信息  
  15.                                 uri = Uri.parse("content://" + path + "/contacts/" + id + "/data");  
  16.                                 Cursor c = resolver.query(uri, new String[]{"mimetype","data1"}, nullnullnull);  
  17.                                 while(c.moveToNext())  
  18.                                 {  
  19. //通过mimetype判断这条信息是什么类型然后输出内容                                        if("vnd.android.cursor.item/name".equals(c.getString(c.getColumnIndex("mimetype"))))  
  20.                                         {  
  21.                                                 Log.i(TAG,"This is Name:" + c.getString(c.getColumnIndex("data1")));  
  22.                                         }  
  23.                                         if("vnd.android.cursor.item/phone_v2".equals(c.getString(c.getColumnIndex("mimetype"))))  
  24.                                         {  
  25.                                                 Log.i(TAG, "This is Phone:" + c.getString(c.getColumnIndex("data1")));  
  26.                                         }  
  27.                                         if("vnd.android.cursor.item/email_v2".equals(c.getString(c.getColumnIndex("mimetype"))))  
  28.                                         {  
  29.                                                 Log.i(TAG, "This is Email:" + c.getString(c.getColumnIndex("data1")));  
  30.                                         }  
  31.                                 }  
  32.                         }  
  33.                 }  

因为是对联系人的数据相关所以需要权限:

  1. <uses-permission android:name="android.permission.READ_CONTACTS"/>  


联系人数据的写入

写入有两种方式

一种是一步一步添加(如果一步出错下一步还会继续执行,这样就有可能导致数据不完整)

  1.                 public void TestInsert()throws Exception{  
  2.                         String path = ContactsContract.AUTHORITY;  
  3. //先添加raw_contacts 类似于先注册一个账号  
  4.                         Uri uri = Uri.parse("content://" + path + "/raw_contacts");  
  5.                         ContentResolver resolver = this.getContext().getContentResolver();  
  6.                         ContentValues values = new ContentValues();  
  7. //得到id  
  8.                         long id=ContentUris.parseId(resolver.insert(uri, values));  
  9. //添加数据  类似于完成账号的详细信息  
  10.                         uri = Uri.parse("content://" + path + "/data");  
  11.                         values.put("raw_contact_id", id); // 添加名字  
  12.                         values.put("mimetype""vnd.android.cursor.item/name");  
  13.                         values.put("data1""陈苒修");  
  14.                         //添加  
  15.                         resolver.insert(uri, values);  
  16. //清空(防止数据残留)  
  17.                         values.clear();  
  18.                          
  19.                         values.put("raw_contact_id", id); // 添加电话  
  20.                         values.put("mimetype""vnd.android.cursor.item/phone_v2");  
  21.                         values.put("data1""5201314");  
  22.                         values.put("data2""1");  
  23.                          
  24.                         resolver.insert(uri, values);  
  25.                         values.clear();  
  26.                          
  27.                         values.put("raw_contact_id", id); // 添加邮箱  
  28.                         values.put("mimetype""vnd.android.cursor.item/email_v2");  
  29.                         values.put("data1""wangsiyu@qq.com");  
  30.                         values.put("data2""1");  
  31.                          
  32.                          
  33.                         resolver.insert(uri, values);  
  34.                 }  

还有一种是一起添加(一步错则不会进行添加)采用的事务


远程事务的处理

相对上面的添加方式,这种添加方式则是先把你要添加的信息添加到一个集合里然后在放入ContentResolver添加提供的方法里,一次性添加。如果有一步添加不成功则会回滚则一条数据都不会添加(该添加方法内部做了数据库的事务处理)

  1.                 public void TestInsert2()throws Exception{  
  2.                         String authority = ContactsContract.AUTHORITY;  
  3. //先添加raw_contacts 类似于先注册一个账号  
  4. //添加空的它默认给你开辟一个联系人的空间  
  5. Uri uri = Uri.parse("content://" + authority + "/raw_contacts");  
  6. ContentResolver resolver = this.getContext().getContentResolver();  
  7. ArrayList<ContentProviderOperation> operations= newArrayList<ContentProviderOperation>();  
  8. ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)  
  9.                         .withValues(new ContentValues()).build();  
  10.                         operations.add(op1);  
  11. //添加数据  类似于完成账号的详细信息  
  12.                          uri = Uri.parse("content://" + authority + "/data");  
  13. //添加三条数据分别代表的意思不一样,但是都是属于一个联系人的所以ID一样  
  14. //联系人姓名  
  15.                         ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)  
  16.                         .withValueBackReference("raw_contact_id"0)  
  17.                         .withValue("mimetype""vnd.android.cursor.item/name")  
  18.                         .withValue("data1""陈苒修1").build();  
  19.                         operations.add(op2);  
  20. //联系人电话  
  21.                         ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)  
  22.                         .withValueBackReference("raw_contact_id"0)  
  23.                         .withValue("mimetype""vnd.android.cursor.item/phone_v2")  
  24.                         .withValue("data1""5201314")  
  25.                         .withValue("data2""1").build();  
  26.                         operations.add(op3);  
  27. //联系人邮箱  
  28.                         ContentProviderOperation op4 = ContentProviderOperation.newInsert(uri)  
  29.                         .withValueBackReference("raw_contact_id"0)  
  30.                         .withValue("mimetype""vnd.android.cursor.item/email_v2")  
  31.                         .withValue("data1""wangsiyu@qq.com")  
  32.                         .withValue("data2""1").build();  
  33.                         operations.add(op4);  
  34.                         resolver.applyBatch(authority, operations);         
  35.                 }  

联系人写入权限

  1. <uses-permission android:name="android.permission.WRITE_CONTACTS"/>  




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多