android中的UriMatcher和ContentUris
来源:互联网 发布:店面装修设计网站 知乎 编辑:程序博客网 时间:2024/06/15 05:18
此篇文章转自 于“http://www.cnblogs.com/lxmanutd/archive/2011/08/14/2138080.html”。总结的非常好,所以记录保存一下,方便以后查阅
两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris。
UriMatcher
用于匹配Uri。
用法如下:
首先把你需要匹配Uri路径全部给注册上:
注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用 addURI()方法传入的第三个参数,例如匹配content://com.test.provider.personprovider/person 路径,返回的匹配码为1。
//常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码
UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//添加需要匹配的uri,如果匹配就会返回匹配码
//如果match()方法匹配content://com.test.provider.personprovider/person路径,返回匹配码为1
sUriMatcher.addURI(“com.test.provider.personprovider”, “person”, 1);
//如果match()方法匹配content://com.test.provider.personprovider/person/530路径,返回匹配码为2
//#号为通配符
sUriMatcher.addURI(“com.test.provider.personprovider”, “person/#”, 2);
switch(sUriMatcher.match(Uri.parse(“content://com.test.provider.personprovider/person/10”))) {
case 1 break;
case 2 break;
default:
//不匹配 break;
}
ContentUris类
用于获取Uri路径后面的ID部分:
1、ContentUris.withAppendedId(Uri contentUri, long id)用于为路径加上ID部分:
Uri uri = Uri.parse(“content://com.test.provider.personprovider/person”)
Uri resultUri = ContentUris.withAppendedId(uri, 5);
//生成后的Uri为:content://com.test.provider.personprovider/person/5
其结果等价于Uri.withAppendedPath(Uri baseUri, String pathSegment)
Uri resultUri = Uri.withAppendedPath(uri, “5”);
2、ContentUris.parseId(uri)方法用于从路径中获取ID部分:
Uri uri = Uri.parse(“content://com.test.provider.personprovider/person/5”)
long personid = ContentUris.parseId(uri);
//获取的结果为:5
ContentResolver
通过URI来添加、删除、修改和查询ContentProvider中提供的数据。除了URI以外,还必须知道需要获取的数据段的名称,以及此数据段的数据类型。如果你需要获取一个特定的记录,你就必须知道当前记录的ID,也就是URI中D部分。
可以使用Activity提供的getContentResolver()方法。
ContentResolver使用insert、delete、update、query方法来操作数据。
1、final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)Query the given URI, returning a Cursor over the result set.
2、final Uri insert(Uri url, ContentValues values)Inserts a row into a table at the given URL.
3、final int update(Uri uri, ContentValues values, String where, String[] selectionArgs)Update row(s) in a content URI.
4、final int delete(Uri url, String where, String[] selectionArgs)Deletes row(s) specified by a content URI.
- android中的UriMatcher和ContentUris
- android中的UriMatcher和ContentUris
- Android -UriMatcher,ContentUris
- Android - UriMatcher ContentUris
- UriMatcher和ContentUris简介
- ContentProvider 和 ContentResolver学习中的UriMatcher 和ContentUris
- 【Android】Uri、UriMatcher、ContentUris详解
- Android Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- 【Android】Uri、UriMatcher、ContentUris详解
- [Android]Uri、UriMatcher、ContentUris详解
- Android:Uri、UriMatcher、ContentUris详解
- [Android]Uri、UriMatcher、ContentUris详解
- Apache POI 导出Excel笔记
- js进制转换问题
- CUDA内存类型memory
- Maven中使用Profile和filtering进行属性过滤
- Java异常:选择Checked Exception还是Unchecked Exception?
- android中的UriMatcher和ContentUris
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- 什么是设计模式
- 第一次接触Java
- 浏览器默认样式的一些看法(User Agent Stylesheet)
- Java学习之反射
- MQTT协议学习及实践
- 关于python sklearn库在windows平台下的一些问题
- js 错误问题