博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用ContentProvider
阅读量:6970 次
发布时间:2019-06-27

本文共 3333 字,大约阅读时间需要 11 分钟。

hot3.png

一. 了解 ContentProvider 

1.   什么是ContentProvider 

    让开发者在多个应用中操作数据,如存储,修改删除 的唯一方式 ,一个ContentProvider 实现 了下面的接口。

ContentProvider.insert( Uri ,ContentValues )ContentProvider.query (Uri ,String [] ,String ,String [], String )ContentProvider.update(Uri .ConentValues .String ,String []);ContentProvider.delete( Uri ,String .String );ContentProvider.getType (Uri );

通过 这些接口,我们不用关心数据 的结构 。

2.   什么是URI

 UUniversal Resources Identifier,在安卓 中,URI有三个部分

(1)  "content://"  ,开头

(2)  数据路径

(3)  ID ,可选 ,如果 不写,所有 的数据 。

content://contacts/peopel

很多常用 的URI安卓 已经定义也常量 。

3.ContentResolver 

      ContentProvider将数据暴露给外面,然后我们用ContentResolve得到数据 。相当 于是一个数据 的消费者,我们用  getContentResolver来得到当前 应用的ContentResolver对象 。

    与ContentProvider一一对应,它有五个接口。

它们将以Cursor的形式返回结果 ,与数据 库相同。

二.  使用ContentProvider

    系统的一些 程序 ,如联系人,通话记录等,往往作为 ContentProvider向外提供 数据 ,我们可以用managedQuery()方法很方便查询相关数据 

1.联系人

三个步骤,我们在这里将Activity   extends ListActivity,重点突出ContentProvider的作用。

(1) 查询联系人,得到Cursor对象 

    managedQuery( Uri uri ,String [] projection ,String selection ,String [] selectionArgs ,String sortOrder)

projection: 要查询的数据 的属性。

(2)新建 一个Adapter

ListAdapter adapter = new SimpleCursorAdapter ( Context context ,int layout ,Cursor c ,String [] from ,int [] to );

(3) 设置Adapter 

  setListAdapter (adapter );

例子:

Cursor c = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,				null,null,null,null);				ListAdapter adapter = new SimpleCursorAdapter (getApplicationContext(), 				android.R.layout.simple_list_item_2, c, 				new String [] {ContactsContract.Contacts.DISPLAY_NAME,			ContactsContract.CommonDataKinds.Phone.NUMBER}, 				new int [] {android.R.id.text1,android.R.id.text2});				setListAdapter (adapter);

在ContactsContract中,我们可以找到所有Contacts的信息。

最后要注意,读取联系人时,要的权限 。

2. 通话记录

和上面的相比 ,要改的是

Uri :CallLog.Calls.CONTENT_URI

还有两个属性: 号码,通话时间

如下:

Cursor c = managedQuery(CallLog.Calls.CONTENT_URI,				null,null,null,null);				ListAdapter adapter = new SimpleCursorAdapter (getApplicationContext(), 				android.R.layout.simple_list_item_2, c, 				new String [] {Calls.NUMBER,Calls.DURATION}, 				new int [] {android.R.id.text1,android.R.id.text2});				setListAdapter (adapter);

3. 多媒体信息

4. 书签

三 .使用ContentResolver 

    外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,来操作数据。

1. 删除 数据 

首先,我们要得到ContentResolver 对象 

ContentResolver resolver = getContentResolver();

使用delete

ContentResolver.delete (Uri  uri   ,String where ,String [] selectionArgs );

如要删除 所有 的联系人:

resolver.delete( Data.CONTENT_URI  ,null ,null );

如果要删除 名字为WES的,如下 

resolver.delete (Data.CONTENT_URI ,StructuredName.DISPLAY_NAME +"=", new String [] {"WES"});

2. 查询数据 

与数据库类似 

ContentResolver.query (Uri  uri ,String [] projection ,String selection ,String [] SelectionArgs ,String sortOrder );

projection :要查询的属性。

如要查询所有人l,的信息。

resolver.query (ContactsContract.CommenDataKinds.Phone,CONTENT_URI , null ,null,null, null);

3. 更新数据

ContentResolver.update (Uri uri ,ContentValues values ,String where ,String [] selectionArgs );

如下例子:

values.put(StructruedName.DISPLAY_NAME ,"WES" );resolver.update(Data.CONTENT_URI , values ,  StruacturedName.DISPLAY_NAME+ "=?", new String [] {"WES"});

4. 插入数据

这个 看起来很容易 ,但是实际 上很难。

ContentResolver .insert( Uri  uri ,ContentValues values );

暂时不写

转载于:https://my.oschina.net/chuiyuan/blog/226064

你可能感兴趣的文章
使用VisualVM、JMC远程监控JVM
查看>>
配置sql server 2000以允许远程访问 及 连接中的四个最常见错误
查看>>
win命令行打包文件(系统自带 ***必备)
查看>>
haproxy的安装与配置
查看>>
DRBD脑裂修复
查看>>
菜鸟如何应对服务器被黑
查看>>
Window7上安装Ruby on Rails
查看>>
Windows Server 8全新的服务器管理器(一)——仪表板
查看>>
我的友情链接
查看>>
运维监控之Zabbix
查看>>
electron 中文文档
查看>>
Kafka文件存储机制那些事
查看>>
cobbler 无人值守安装
查看>>
VS2010快捷键
查看>>
Java Map接口
查看>>
Cron表达式
查看>>
用VBA实现Ooutlook收到邮件后自动转发外部邮箱
查看>>
ls command not found
查看>>
UML类图几种关系的总结
查看>>
学习数据结构
查看>>