第七章 跨内容共享数据,探究内容提供器
2016-02-03 01:42
435 查看
7.1内容提供器简介
内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能.内容提供器可以选择指定数据进行共享.使用方式包括使用现有的内容提供器读取和操作数据,或者创建自己的内容提供器给数据提供外部访问的接口.7.2访问其他程序的接口
当应用程序通过内容提供器对其数据提供了外部访问的接口,任何其他的应用程序就可以对这部分数据进行访问.7.2.1ContentResolver的基本用法
要借助ContentResolve类,通过Context中的getContentResolver()方法获取该类的实例.ContentResolver中提供了方法进行增删改查操作.- insert()用于添加数据.
- update()用于更新数据.
- delete()用于删除数据.
- query()用于查询数据.
步骤:
1.ContentResolver中的增删改查方法都不接收表名参数,而使用一个Uri参数代替,这个参数称为内容URI.内容URI为内容提供器的数据建立了唯一的标识符.内容URI由权限(authority)和路径(path)组成.权限用于对不同的应用程序做区分,通常用程序包名命名(避免冲突).路径是用于对同一应用程序中不同的表做区分,通常添加到取新鲜后面.内容URI的标准写法是:
content://com.example.app.provider/table1
2.获取到的字符串需要解析为URI对象:
Uri uri = Uri.parse(“content://com.example.app.provider/table1”);
3.利用Uri对象查询数据.
Cursor cursor = getContentResolver().
query(uri,projection,selection,selectionArgs,sortOrder);
query()方法参数 | 对应 SQL 部分 | 描述 |
---|---|---|
uri | from table_name | 指定查询某个应用程序下的某一张表 |
projection | select column1, column2 | 指定查询的列名 |
selection | where column = value | 指定 where 的约束条件 |
selectionArgs | 为 where 中的占位符提供具体的值 | |
orderBy | order by column1, column2 | 指定查询结果的排序方式 |
查找:
if (cursor != null) { while (cursor.moveToNext()) { String column1 = cursor.getString(cursor.getColumnIndex("column1")); int column2 = cursor.getInt(cursor.getColumnIndex("column2")); } cursor.close(); }
增加:
ContentValues values = new ContentValues(); values.put("column1", "text"); values.put("column2", 1); getContentResolver().insert(uri, values);
修改:
ContentValues values = new ContentValues(); values.put("column1", ""); getContentResolver().update(uri, values, "column1 = ? and column2 = ?", newString[] {"text", "1"});
删除:
getContentResolver().delete(uri, "column2 = ?", new String[] { "1" });
7.2.2读取系统联系人
发生了点意外(悲伤脸7.3创建自己的内容提供器
7.3.1创建内容提供器的步骤
创建一个类继承ContentProvider并重写- onCreate():初始化内容提供器时调用,用于数据库的创建和升级等操作返回true则初始化成功,false则失败.只有ContentResolver尝试访问程序中的数据时,内容提供器才会被初始化.
- query():使用 uri 参数来确定查询哪张表, projection 参数用于确定查询哪些列,selection 和 selectionArgs 参数用于约束查询哪些行,sortOrder 参数用于对结果进行排序, 查询的结果存放在 Cursor 对象中返回.
- insert():向内容提供器中添加一条数据,使用 uri 参数来确定要添加到的表,待添加的数据保存在 values 参数中,添加完成后,返回一个用于表示这条新记录的 URI.
- update():更新内容提供器中已有的数据.使用 uri 参数来确定更新哪一张表中的数据,新数据保存在 values 参数中,,selection 和 selectionArgs 参数用于约束更新哪些行,,受影响的行数将作为返回值返回.
- delete():从内容提供器中删除数据.使用 uri 参数来确定删除哪一张表中的数据,和 selectionArgs 参数用于约束删除哪些行, 被删除的行数将作为返回值返回.
- getType():根据传入的内容 URI 来返回相应的 MIME 类型.
在内容URI后可以加ID号,返回的则是相应ID的数据.
可以使用通配符来匹配URI:
- *:任意长度的字符
- #:任意长度的数字
借助URIMatcher可以实现内容URI的匹配.URIMatcher提供了addURI(),接受三个参数(权限.路径,自定义代码).返回一个匹配这个人Uri对象所对应的自定义代码.根据代码可知调用方访问的是那张表的数据.
public class MyProvider extends ContentProvider { public static final int TABLE1_DIR = 0; public static final int TABLE1_ITEM = 1; public static final int TABLE2_DIR = 2; public static final int TABLE2_ITEM = 3; private static UriMatcher uriMatcher; static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI("com.example.app.provider", "table1", TABLE1_DIR); uriMatcher.addURI("com.example.app.provider ", "table1/#", TABLE1_ITEM); uriMatcher.addURI("com.example.app.provider ", "table2", TABLE2_ITEM); uriMatcher.addURI("com.example.app.provider ", "table2/#", TABLE2_ITEM); } …… @Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) { switch (uriMatcher.match(uri)) { case TABLE1_DIR: // 查询table1表中的所有数据 break; case TABLE1_ITEM: // 查询table1表中的单条数据 break; case TABLE2_DIR: // 查询table2表中的所有数据 break; case TABLE2_ITEM: // 查询table2表中的单条数据 break; default: break; } …… } …… }
getType()是所有内容提供器都必须提供的方法.用于获取Uri对象对应的MIME类型.MINE由三部分组成:
必须以 vnd 开头。
如果内容 URI 以路径结尾,则后接 android.cursor.dir/,如果内容 URI 以 id 结尾,则后接 android.cursor.item/。
最后接上 vnd..。
content://com.example.app.provider/table1
==>vnd.android.cursor.dir/vnd.com.example.app.provider.table1
content://com.example.app.provider/table1/1 ==>vnd.android.cursor.item/vnd. com.example.app.provider.table1
对于隐私数据不提供对应的URI外部程序就无法访问了.
7.3.2 实现跨程序数据共享
![](http://upload-images.jianshu.io/upload_images/1504266-3a68e3a4488be152.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
略略略
相关文章推荐
- 算法导论例程——最大子数组问题
- Android客户端与服务器端交互数据之json解析
- 猴年回家前的思绪
- UVALive 3634 熟悉一下STL
- JAVA中的包装类
- Leetcode(1) Two Sum
- 起点——2015年终总结
- addr2line 工具
- java面向对象的基础扫描
- 学习新技术方法感想
- ICPCCamp 2016 Day1 ftiasch's Contest #4(Around the world-树,构造)
- 【HDOJ】2890 Longest Repeated subsequence
- linux文件I/O
- DNS 主从实验
- JAVA中的匿名内部类
- python 多线程笔记(5)-- 生产者/消费者模式
- js获取浏览器高度和宽度值(多浏览器)
- DNS and BIND 笔记总结
- 计算A+B及其结果的标准形式输出
- 个人笔记 html 08 浏览器返回页面时刷新