内容提供器--2、创建内容提供器
来源:互联网 发布:linux切换图形界面命令 编辑:程序博客网 时间:2024/06/05 23:06
1、新建一个类去继承 ContentProvider 的方式来创建一个自己的内容提供器
2、ContentProvider 类中有六个抽象方法,我们在使用子类继承它的时候,需要将这六个方法全部重写。
- onCreate()
- query()
- insert()
- update()
- delete()
- getType():根据传入的内容URI来返回相应MIME类型
3、借助UriMatcher这个类可轻松实现匹配内容URI的功能, UriMatcher提供 addURI()方法,把权限,路径和自定义代码传进去。
通配符:
content://com.example.app.provider/*
*表示匹配任意长度的任意字符
content://com.example.app.provider/table1/#
表示匹配任意长度的数字
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);}
4、根据调入的URI返回table表中的数据
@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;}
5、使用getType()方法,用于获取 Uri 对象所对应的 MIME 类型字符串。
- 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
@Overridepublic String getType(Uri uri) {switch (uriMatcher.match(uri)) {case TABLE1_DIR:return "vnd.android.cursor.dir/vnd.com.example.app.provider.table1";case TABLE1_ITEM:return "vnd.android.cursor.item/vnd.com.example.app.provider.table1"; case TABLE2_DIR:return "vnd.android.cursor.dir/vnd.com.example.app.provider.table2";case TABLE2_ITEM:return "vnd.android.cursor.item/vnd.com.example.app.provider.table2";default:break;}return null;}
0 0
- 内容提供器--2、创建内容提供器
- Android创建内容提供器
- Android 内容提供器---创建内容提供器(概要)
- Android 内容提供器---创建内容提供器(概要)
- Android 内容提供器---创建内容提供器(实现内容提供器权限)
- Android 内容提供器---创建内容提供器(实现内容提供器权限)
- 创建自己的内容提供器ContentProvider
- 创建自己的内容提供器
- Andriod---创建自己的内容提供器
- ContentProvider ---内容提供器
- android内容提供器
- ContentProvider内容提供器
- 内容提供器
- 内容提供器Demo
- 5.内容提供器
- 探究内容提供器
- ContentProvider(内容提供器)
- 内容提供器
- CloudSimExample1-仿真步骤
- 动态配置连接数据库
- 循环链表 ——解决约瑟夫环
- linux里的shell环境变量
- IE下判断IE版本的语句...[if lte IE 8]……[endif]
- 内容提供器--2、创建内容提供器
- PHP学习笔记:1.PHP基本语法详解整型
- CloudSim源码分析-CloudSim的初始化
- Java JSP 内置对象
- VS 2012问题集锦
- 利用NSString的Hash方法比较字符串
- BZOJ 3668 [Noi2014]起床困难综合症 贪心+位运算
- XML学习笔记一
- TCP的流量控制和拥塞控制