ContentProvider的一些总结
2015-09-06 15:03
501 查看
ContentProvider中的URI,
The URI that identifies the provider 一个特定的uri对应着唯一一个内容提供者,
谷歌官方文档里的说明,
The URI that identifies the provider
The names of the data fields you want to receive
The data types for those fields
If you're querying a particular record, you also need the ID for that record.
The first argument to either
To restrict a query to just one record, you can append the
There are some helper methods, particularly
The URI that identifies the provider 一个特定的uri对应着唯一一个内容提供者,
谷歌官方文档里的说明,
Querying a Content Provider
You need three pieces of information to query a content provider:The URI that identifies the provider
The names of the data fields you want to receive
The data types for those fields
If you're querying a particular record, you also need the ID for that record.
Making the query
To query a content provider, you can use either the, java.lang.String, java.lang.String[], java.lang.String)]ContentResolver.query()method or the
, java.lang.String, java.lang.String[], java.lang.String)]Activity.managedQuery()method. Both methods take the same set of arguments, and both return a Cursor object. However,
managedQuery()causes the activity to manage the life cycle of the Cursor(使用managedQuery()这个方法,会促使Cursor的生命周期受制于Activity). A managed Cursor handles all of the niceties, such as unloading itself when the activity pauses, and requerying itself when the activity restarts. You can ask an Activity to begin managing an unmanaged Cursor object for you by calling
Activity.startManagingCursor().
The first argument to either
, java.lang.String, java.lang.String[], java.lang.String)]query()or
, java.lang.String, java.lang.String[], java.lang.String)]managedQuery()is the provider URI — the
CONTENT_URIconstant that identifies a particular ContentProvider and data set (see URIsearlier).
To restrict a query to just one record, you can append the
_IDvalue for that record to the URI — that is, place a string matching the ID as the last segment of the path part of the URI. For example, if the ID is 23, the URI would be:
content://. . . ./23
There are some helper methods, particularly
ContentUris.withAppendedId()and
Uri.withAppendedPath(), that make it easy to append an ID to a URI. Both are static methods that return a Uri object with the ID added. So, for example, if you were looking for record 23 in the database of people contacts, you might construct a query as follows:
import android.provider.Contacts.People; import android.content.ContentUris; import android.net.Uri; import android.database.Cursor; // Use the ContentUris method to produce the base URI for the contact with _ID == 23. Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23); // Alternatively, use the Uri method to produce the base URI. // It takes a string rather than an integer. Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23"); // Then query for this specific record: Cursor cur = managedQuery(myPerson, null, null, null, null);
相关文章推荐
- php——<<<start运算符
- php里日期的部分显示
- php中日期时间的用法
- php计算几分钟前、几小时前等
- PHPMailer通过Gmail和Office365的SMTP账号发送邮件设置
- thinkphp独立分组后分页功能不能用的解决办法
- vsftpd配置好后,仍然出现无法下载文件的情况
- Eclipse的PHP插件PHPEclipse安装和使用
- LeapFTP 显示服务器端文件名乱码问题处理
- [转] rtp h264注意点(FU-A分包方式说明)
- IIS FTP文件服务器搭建步骤
- PHP PDO函数库详解
- phpMyAdmin4.4.7安装及配置
- phpMyAdmin4.4.7安装及配置
- php数组与字符串的转换
- php xss filter
- laravel子域名和主站同站
- phpMailer在thinkPHP框架中邮件发送
- vsftpd的配置详解(中文说明)
- PHP安装pthreads多线程扩展教程[windows篇]