转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!
前言
手机内部其实就是用我们学过的Sqlite数据库存储我们的常用数据,如:手机联系人,手机上面的图片地址,手机铃声等等,接下来我们来学习如何读写手机中的联系人数据。
联系人数据的读取和写入
了解联系人数据的存储方式
以数据库(sqlite)存储主要使用到三张表
联系人数据的读取
- public void TestQuery()throws Exception{
- // 得到域名(通过观察源码得知)
- String path = ContactsContract.AUTHORITY;
- // 操作raw_contacts
- Uri uri = Uri.parse("content://" + path + "/raw_contacts");
- ContentResolver resolver = this.getContext().getContentResolver();
- //查询id 并且只查询没有删除的
- Cursor cursor = resolver.query(uri, new String[]{"_id"}, "deleted=?", new String[]{"0"}, null);
- while(cursor.moveToNext())
- {
- //得到ID
- String id = cursor.getString(0);
- Log.i(TAG, "This is id:" + cursor.getString(0));
- //通过ID我们在到data表里面查询关于该ID相关信息
- uri = Uri.parse("content://" + path + "/contacts/" + id + "/data");
- Cursor c = resolver.query(uri, new String[]{"mimetype","data1"}, null, null, null);
- while(c.moveToNext())
- {
- //通过mimetype判断这条信息是什么类型然后输出内容 if("vnd.android.cursor.item/name".equals(c.getString(c.getColumnIndex("mimetype"))))
- {
- Log.i(TAG,"This is Name:" + c.getString(c.getColumnIndex("data1")));
- }
- if("vnd.android.cursor.item/phone_v2".equals(c.getString(c.getColumnIndex("mimetype"))))
- {
- Log.i(TAG, "This is Phone:" + c.getString(c.getColumnIndex("data1")));
- }
- if("vnd.android.cursor.item/email_v2".equals(c.getString(c.getColumnIndex("mimetype"))))
- {
- Log.i(TAG, "This is Email:" + c.getString(c.getColumnIndex("data1")));
- }
- }
- }
- }
因为是对联系人的数据相关所以需要权限:
- <uses-permission android:name="android.permission.READ_CONTACTS"/>
联系人数据的写入
写入有两种方式
一种是一步一步添加(如果一步出错下一步还会继续执行,这样就有可能导致数据不完整)
- public void TestInsert()throws Exception{
- String path = ContactsContract.AUTHORITY;
- //先添加raw_contacts 类似于先注册一个账号
- Uri uri = Uri.parse("content://" + path + "/raw_contacts");
- ContentResolver resolver = this.getContext().getContentResolver();
- ContentValues values = new ContentValues();
- //得到id
- long id=ContentUris.parseId(resolver.insert(uri, values));
- //添加数据 类似于完成账号的详细信息
- uri = Uri.parse("content://" + path + "/data");
- values.put("raw_contact_id", id); // 添加名字
- values.put("mimetype", "vnd.android.cursor.item/name");
- values.put("data1", "陈苒修");
- //添加
- resolver.insert(uri, values);
- //清空(防止数据残留)
- values.clear();
-
- values.put("raw_contact_id", id); // 添加电话
- values.put("mimetype", "vnd.android.cursor.item/phone_v2");
- values.put("data1", "5201314");
- values.put("data2", "1");
-
- resolver.insert(uri, values);
- values.clear();
-
- values.put("raw_contact_id", id); // 添加邮箱
- values.put("mimetype", "vnd.android.cursor.item/email_v2");
- values.put("data1", "wangsiyu@qq.com");
- values.put("data2", "1");
-
-
- resolver.insert(uri, values);
- }
还有一种是一起添加(一步错则不会进行添加)采用的事务
远程事务的处理
相对上面的添加方式,这种添加方式则是先把你要添加的信息添加到一个集合里然后在放入ContentResolver添加提供的方法里,一次性添加。如果有一步添加不成功则会回滚则一条数据都不会添加(该添加方法内部做了数据库的事务处理)
- public void TestInsert2()throws Exception{
- String authority = ContactsContract.AUTHORITY;
- //先添加raw_contacts 类似于先注册一个账号
- //添加空的它默认给你开辟一个联系人的空间
- Uri uri = Uri.parse("content://" + authority + "/raw_contacts");
- ContentResolver resolver = this.getContext().getContentResolver();
- ArrayList<ContentProviderOperation> operations= newArrayList<ContentProviderOperation>();
- ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
- .withValues(new ContentValues()).build();
- operations.add(op1);
- //添加数据 类似于完成账号的详细信息
- uri = Uri.parse("content://" + authority + "/data");
- //添加三条数据分别代表的意思不一样,但是都是属于一个联系人的所以ID一样
- //联系人姓名
- ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)
- .withValueBackReference("raw_contact_id", 0)
- .withValue("mimetype", "vnd.android.cursor.item/name")
- .withValue("data1", "陈苒修1").build();
- operations.add(op2);
- //联系人电话
- ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)
- .withValueBackReference("raw_contact_id", 0)
- .withValue("mimetype", "vnd.android.cursor.item/phone_v2")
- .withValue("data1", "5201314")
- .withValue("data2", "1").build();
- operations.add(op3);
- //联系人邮箱
- ContentProviderOperation op4 = ContentProviderOperation.newInsert(uri)
- .withValueBackReference("raw_contact_id", 0)
- .withValue("mimetype", "vnd.android.cursor.item/email_v2")
- .withValue("data1", "wangsiyu@qq.com")
- .withValue("data2", "1").build();
- operations.add(op4);
- resolver.applyBatch(authority, operations);
- }
联系人写入权限
- <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
|