Android中的UriMatcher、ContentUrist和ContentResolver
因为Uri代表了要操作的数据,所以我们很经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。掌握它们的使用,会便于我们的开发工作。
UriMatcher:用于匹配Uri,它的用法如下:
1.首先把你需要匹配Uri路径全部给注册上,如下:
Java代码:
- //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)。
- UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- //如果match()方法匹配content:.sqlite.provider.contactprovider/contact路径,返回匹配码为1
- uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact”, 1);
- //添加需要匹配uri,如果匹配就会返回匹配码
- //如果match()方法匹配 content.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2
- uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact/#”, 2);
- //#号为通配符
复制代码
外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver
类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。
ContentResolver使用insert、delete、update、query方法,来操作数据。
- package eoe.sqlite.provider;
- import com.changcheng.sqlite.MyOpenHelper;
- import android.content.ContentProvider;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.UriMatcher;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.net.Uri;
- public class ContactContentProvider extends ContentProvider {
- // 通过UriMatcher匹配外部请求
- private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- // 通过openHelper进行数据库读写
- private MyOpenHelper openHelper;
- // 匹配状态常量
- private static final int CONTACT_LIST = 1;
- private static final int CONTACT = 2;
- // 表名
- private static final String tableName = "contacts";
- // 添加Uri
- static {
- uriMatcher.addURI("com.changcheng.sqlite.provider", "contact",CONTACT_LIST);
- uriMatcher.addURI("com.changcheng.sqlite.provider", "contact/#",CONTACT);
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- SQLiteDatabase db = this.openHelper.getWritableDatabase();
- int result;
- switch (uriMatcher.match(uri)) {
- case CONTACT_LIST:
- result = db.delete(tableName, selection, selectionArgs);
- break;
- case CONTACT:
- long id = ContentUris.parseId(uri);
- String where = "_id=" + id;
- if (selection != null && !"".equals(selection)) {
- where = where + " and " + selection;
- }
- result = db.delete(tableName, where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
- }
- return result;
- }
- @Override
- public String getType(Uri uri) {
- switch (uriMatcher.match(uri)) {
- case CONTACT_LIST:// 集合类型必须在前面加上vnd.android.cursor.dir/
- return "vnd.android.cursor.dir/contactlist";
- case CONTACT:// 非集合类型必须在前面加上vnd.android.cursor.item/
- return "vnd.android.cursor.item/contact";
- default:
- throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
- }
- }
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = this.openHelper.getWritableDatabase();
- long id;
- switch (uriMatcher.match(uri)) {
- case CONTACT_LIST:
- // 因为后台需要生成SQL语句,当values为null时,必须提第二个参数。生成的SQL语句才不会出错!
- id = db.insert(tableName, "_id", values);
- return ContentUris.withAppendedId(uri, id);
- case CONTACT:
- id = db.insert(tableName, "_id", values);
- String uriPath = uri.toString();
- String path = uriPath.substring(0, uriPath.lastIndexOf("/")) + id;
- return Uri.parse(path);
- default:
- throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
- }
- }
- @Override
- public boolean onCreate() {
- this.openHelper = new MyOpenHelper(this.getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- SQLiteDatabase db = this.openHelper.getWritableDatabase();
- switch (uriMatcher.match(uri)) {
- case CONTACT_LIST:
- return db.query(tableName, projection, selection, selectionArgs,null, null, sortOrder);
- case CONTACT:
- long id = ContentUris.parseId(uri);
- String where = "_id=" + id;
- if (selection != null && !"".equals(selection)) {
- where = where + " and " + selection;
- }
- return db.query(tableName, projection, where, selectionArgs, null,null, sortOrder);
- default:
- throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
- }
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
- SQLiteDatabase db = this.openHelper.getWritableDatabase();
- int result;
- switch (uriMatcher.match(uri)) {
- case CONTACT_LIST:
- result = db.update(selection, values, selection, selectionArgs);
- break;
- case CONTACT:
- long id = ContentUris.parseId(uri);
- String where = "_id=" + id;
- if (selection != null && !"".equals(selection)) {
- where = where + " and " + selection;
- }
- result = db.update(tableName, values, where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Uri IllegalArgument:" + uri);
- }
- return result;
- }
- }
复制代码
Android中的UriMatcher、ContentUrist和ContentResolver的更多相关文章
- Android中的5种数据存储方式
本文转自 http://hi.baidu.com/maguowei/blog/item/7aca46c25574a33ae5dd3ba4.htmlAndroid数据存储Android提供了5种方式存 ...
- Android学习记录(3)—Android中ContentProvider的基本原理学习总结
一.ContentProvider简介 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据 ...
- Android中的跨进程通信方法实例及特点分析(二):ContentProvider
1.ContentProvider简单介绍 在Android中有些数据(如通讯录.音频.视频文件等)是要供非常多应用程序使用的.为了更好地对外提供数据.Android系统给我们提供了Content P ...
- Android中内容提供者ContentProvider的详解
1.什么是ContentProvider 首先,ContentProvider(内容提供者)是android中的四大组件之一,但是在一般的开发中,可能使用的比较少. ContentProvider为不 ...
- Android中内容观察者的使用---- ContentObserver类详解 (转)
前言: 工作中,需要开启一个线程大量的查询某个数据库值发送了变化,导致的开销很大,后来在老大的指点下,利用了 ContentObserver完美的解决了该问题,感到很兴奋,做完之后自己也对Conten ...
- 【转】Android中实现IPC的几种方式详细分析及比较
1.使用Bundle ----> 用于android四大组件间的进程间通信android的四大组件都可使用Bundle传递数据 所以如果要实现四大组件间的进程间通信 完全可以使用Bundl ...
- android中的ContentProvider实现数据共享
为了在应用程序之间交换数据,android中提供了ContentProvider,ContentProvider是不同应用程序之间进行数据交换的标准API.当一个应用程序需要把自己的数据暴露给其他程序 ...
- android 中获取视频文件的缩略图(非原创)
在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...
- 一个Demo学完Android中所有的服务(转)
说明:这个例子实现了Android中常见的许多服务,下面是实现的截图 接下来,以源代码的方式分析这个例子 1.MainActivity--主界面 这个类主要是实现用户所看到的这个Activity, ...
随机推荐
- [转]Android的ADT与SDK的区别
adt只是一个eclipse的插件,里面可以设置sdk路径 SDK(Software Development Kit): 一般是一些被软件工程师用于为特定的软件包.软件框架.硬件平台.操作 ...
- Alpha冲刺(6/10)——追光的人
1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...
- MYSQL5上运行多个实例
date 20131005参考http://chenzehe.iteye.com/blog/1266260官方文档 http://dev.mysql.com/doc/refman/5.1/zh/dat ...
- 转载:ArcEngine二次开发界面基本设置
转自:https://blog.csdn.net/weixin_42032107/article/details/80644991 1. 在form窗体中添加菜单栏和状态栏控件 2. 添加li ...
- 【java】代码优化点
1.对数据库数据的计数统计,尽量在数据库查询时候就使用count()进行统计,避免返回List到项目中统计List大小 2.对于数据库中表中字段数据过长,例如存储的是text类型而不是verchar类 ...
- RES协议
MFC 通过HTML访问内部资源 资源导入JPG的图片,资源对应ID是137 <img src="res:/JPG/#137" width="100%" ...
- 深入分析JavaWeb Item40 -- 文件上传和下载
在Web应用系统开发中,文件上传和下载功能是很经常使用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传.浏览器在上传的过程中是将文件以流的形式提交到server端的.假设 ...
- find . -mtime +1 查找文件
N * 24+1 内表示 1 * 24 +24小时以外..+0 才表示 0 * 24 +24小时以外1 表示 1*24 + 24 到 24 之间..0 表示 0*24 + 24 到 0 之间..-1 ...
- Linux内核开发者峰会照的全家福
刚才看到一张Linux内核开发者峰会照的全家福,有历史价值,给大家分享一下.上面有Torvalds(大致在中间).Andrew Morton(目前的内核主要维护者,第二排右数第二个).Alan Cox ...
- WAF防御能力评测及工具
本篇文章介绍如何从常规攻击的防御能力来评测一款WAF.一共覆盖了十六种攻击类型,每种类型均从利用场景(攻击操作的目的),注入点(漏洞产生的地方,比如说大多数WAF都会较全面地覆盖来自GET请求的攻击, ...