Uri工具类介绍
UriMatcher工具类介绍
ContentUris介绍
Uri工具类介绍
A:Uri是干什么的?
关于Uri是什么,这里只是简单的介绍下,全名为“统一资源标识符“,具体他下面还有url,以及他们的区别,这里不再介绍,以后再总结。为了方便下面对ContentProvider对象Uri的理解,这里简单介绍下Uri子类Url:
Url最常见于互联网网址:http://www.baidu.com:80/index.asp 这里http://协议部分,www.baidu.com:80域名部分,index.asp是网站具体页面资源部分,下面介绍的Uri与这个结构类似。
B:ContentProvider对象中的Uri的作用?有哪些部分组成?
一个常见的ContentProvider中的Uri如下:
content://com.example.mycontentprovider/words 或者 content://com.example.mycontentprovider/word/1
这里有三部分:
第一部分:content://这个部分是ContentProvider规定的,记住它吧
第二部分:com.example.mycontentprovider这个部分是ContentProvider 的authorities部分,根据这个部分ContentResolver就能找到ContentProvider。
第三部分:words或者word/detail部分,这个是具体的资源部分,当访问者需要访问不同资源时,这部分是变化的。
关于Uri在ContentProvider的作用请参考(http://blog.youkuaiyun.com/miao_dingxiao/article/details/53197986)
C:Android中的Uri几种特殊形式介绍。
形式1:content://com.example.mycontentprovider/words //访问全部数据
形式2:content://com.example.mycontentprovider/word/2 //访问word数据张ID为2的数据
形式3:content://com.example.mycontentprovider/word/2/word //访问word数据中ID为2的记录的word字段
D:String与Uri如何进行类型转换?
转换:String 转换成 Uri
Uri uri = new Uri(String string);
UriMatcher工具类介绍
A:什么是UriMatcher?
UriMatcher字面理解就是地址匹配类,他为了方便数据访问者ContentResolver的意图进行分类,所以在数据暴露者ContentProvider中对参数Uri进行判断,同时也是提前确定数据暴露者实际能处理的Uri,根据Uri参数来确定所操作的数据。
具体见下图:
B:如何创建UriMatcher?
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);//就这么简单
matcher.addURI();//就可以添加ContentProvider根据传递过来的Uri能执行那些方法了
D:举例说明UriMatcher的两个静态方法addURI()/match()
void addURI(String authority,String path,int code);
int match(Uri uri);
eg:
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURi("com.example.mycontentprovider","words",1);
matcher.addURI("com.example.mycontentprovider","word/#",2);
matcher.match("content://com.example.mycontentprovider/words") return 1;
matcher.match("content://com.example.mycontentprovider/word/2") return 2;
matcher.match("content://com.example.mycontentprovider/word/100") return 2;
ContentUris介绍
A:什么是ContentUris工具类?
ContentUris是一个操作Uri 字符串的工具类。
B:他有哪些使用方法?
ContentUris.withAppendedId(Uri uri,int id) return Uri
ContentUris.parseId(Uri uri) return long
C:来个简单的实例
Uri uri = Uri.parse("content://com.example.mycontentprovider/word");
Uri resultUri = ContentUrils.appendedId(uri,2);
//resultUri = content://com.example.mycontentprovider/word/2;
long wordId = ContentUrils.parseId(uri);
//wordId = 2;