android中的UriMatcher和ContentUris
来源:互联网 发布:淘宝红包活动名称 编辑:程序博客网 时间:2024/06/07 09:41
android中的UriMatcher和ContentUris
两个用于操作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详解
- JTable常见用法细则
- instanceof
- 纸牌游戏思路
- android 登录Javaeye(使用HttpURLConnection和HttpClient)
- 基础的类的声明及使用/方法
- android中的UriMatcher和ContentUris
- Premature end of script headers 错误的终极处理方法 (CGI的相关程序)
- yum 配置
- MySQL中select * for update锁表的问题
- JS获取浏览器高度和宽度
- J2EE应用讲解(4)——EJB基础知识
- mount
- 讨人喜欢的 MySQL replace into 用法(insert into 的增强版)
- 如何在linux下编译并执行C程序