首页 技术 正文
技术 2022年11月18日
0 收藏 499 点赞 4,546 浏览 2685 个字

ContentProvider提供数据

在Android中,他的每个应用都是相互独立的,各自运行在自己的Dalvik虚拟机中,但现实使用中常常需要在多个应用之间进行数据交换,例如发短信需要获取联系人中的内容,QQ上传头像需要获取相册内容等等,Android为这种跨应用的数据交换提供了一个标准ContentProvider,用户实现自己的ContentProvider时,需要实现他规定的抽象方法:

public boolean onCreate():该方法在ContentProvider创建后会被调用,当其他应用第一次访问ContentProvider时,该ContentProvider会被创建出来,并立即回调该onCreate()方法。

         public Uri inert(Uri uri,ContentValues values):根据该Uri插入values对应的数据。

public int delete(Uri uri,String selection,String[] selectionArgs):根据Uri删除select条件所匹配的全部记录。

         public int update(Uri uri,ContentValues values ,String selection,String[] selectionArgs):根据Uri修改select条件所匹配的全部记录

public Cursor query(Uri uri ,String[] projection,String selection ,String[] selectionArgs,String sortOrder):根据Uri查询出select条件所匹配的全部记录,其中projection就是列明列表,表明只选择出指定的数据列。

public String getTypt(Uri uri)该方法用于返回当前Uri所代表的数据的MIME类型,如果该Uri对应数据可能包括多条数据,那么MIME类型字符串应该以vnd.android.cursor.dir/开头,如果该Uri对应的数据只包含一条记录,那么返回MIME类型字符串应该以vnd.android.cursor.item/开头。

什么是URI

上面每个方法都有一个Uri参数,那么Uri又是什么呢?

关于Uri的可以参考这两篇博文  http://www.cnblogs.com/gaojing/archive/2012/02/04/2413626.html

http://www.cnblogs.com/hust-ghtao/p/4724885.html

就以上面两个网址来说:

http://这个是URL的协议部分,只要通过http协议来访问网站,前面都是这个前缀。

www.cnblogs.com这个是域名,访问指定的网站,域名部分也是不变的

gaojing/archive/2012/02/04/2413626.htmlhust-ghtao/p/4724885.html这个就是网站资源部分,这个是动态改编的,访问者访问不同的资源,这个部分也会不同。

ContentProvider要求的Uri和URL类似,例如

         content://org.crazyit.providers.dictprovider/words

content://这个是android的contentprovider规定的,就像http://一样,提供ContentProvider和访问ContentProvider的默认协议就是content://

org.crazyit.providers.dictprovider这个类似于网址,系统通过这个名字找到你要操作哪个ContentProvider。

words这个是资源部分,访问者访问不同的资源时候,这里也是动态改变的。

可以使用Uri的静态方法parse()将字符串转换成一个Uri。

ContentResolver访问使用

通常与ContentProvider结合使用的是ContentResolver,一个应用程序使用ContentProvider提供数据,另外一个应用程序使用ContentResolver来访问数据。

可以使用Context的getContentResolver()方法来获取ContentProvider对象

获得了ContentResolver对象之后,可以使用如下方法操作数据:

insert(Uri uri,ContentValues values):向Uri对应的ContentProvider中插入values对应的数据。

delete(Uri uri,String where,String[] selectionArgs):删除Uri对应的ContentProvider中where提交匹配的数据。

update(Uri uri,ContentValues values,String where ,String[] selectionArgs):更新Uri对应的ContentProvider中where提交匹配的数据。

query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder):查询Uri对应的ContentProvider中where提交匹配的数据。

参数说明:

projection:要查询的列名

selection:查询条件子句,相当于select语句where关键字后面的部分,在条件子句允许使用占位符“?”

selectionArgs:对应与selection语句占位符的值,值在数组的位置与占位符在语句中的位置必须一致,否则就会有异常

sortOrder:相当于select语句order by关键字后面的 部分

一般来说,ContentProvider是单例模式,当多个应用程序通过ContentResolver来操作ContentProvider提供的数据时,ContentResolver调用的数据操作将会委托给同一个ContentProvider处理。

注册ContentProvider

在定义好ContentProvider之后,还需要在系统中注册这个ContentProvider,具体操作方法是在AndroidMainifest.xml文件中添加一个<provider>元素,需要注意的是,我们要设置这个元素的exported的值为true,表明这个ContentProvider可以向外部应用提供数据

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898