Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access.
ContentProvider 存储和检索数据,这是在应用之间共享数据的唯一方式。他没有共享的存储区域,并且全部的Android的Packages都能穿过。
Android提供了大量的数据,算然有些需要权限。
This document is an introduction to using content providers. After a brief discussion of the fundamentals, it explores how to query a content provider, how to modify data controlled by a provider, and how to create a content provider of your own.
这个文档提供了一个关于使用Content Provider的介绍,在介绍一些功能之后,还介绍了如果查询Content Provider和如何通过Provider修改数据,并且如何建立自己的Provider。
Content Provider Basics
How a content provider actually stores its data under the covers is up to its designer. But all content providers implement a common interface for querying the provider and returning results — as well as for adding, altering, and deleting data.
It's an interface that clients use indirectly, most generally throughContentResolver
objects. You get a ContentResolver by calling
from within the implementation of an Activity or other application component:getContentResolver()
ContentProvider 存储数据都被设计者封装起来,ContentProvider都实现了查询和返回结果,可以通过调用Activity的ContentResolver对象的getContentResolver() 来调用。
ContentResolver cr = getContentResolver();
URIs
Each content provider exposes a public URI (wrapped as aUri
object) that uniquely identifies its data set. A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one. All URIs for providers begin with the string "content://
". Thecontent:
scheme identifies the data as being controlled by a content provider.
每一个ContentProvider 都有一个piblic的URI(包装成URI对象)作为唯一标示,这个ContentProvider控制了很多的数据和他们不同的表示,所有的Provider的URI都有着一样的开头 “content://”,这个“content:” 这样定义的说明这个数据受content provider 控制。
Making the query
To query a content provider, you can use either the
method or theContentResolver.query()
method. Both methods take the same set of arguments, and both return a Cursor object. However,Activity.managedQuery()
managedQuery()
causes the activity to manage the life cycle of the Cursor. 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()
查询ContentProvider ,你可以用ContentResolver.query() 和 Activity.managedQuery()这两种方法,这两种方法有着相同的参数设置,并且都返回的是Cursor对象,不过,managedQuery()调用的是activity去管理Cursor的生命周期,一个被管理的Cursor将回handle他的所有变化。例如,如果activity pause,它将不再调用自己,如果activity restart,它将requery。同样也可以让activity通过调用Activity.startManagingCursor()来管理一个没有被管理的Cursor。
To restrict a query to just one record, you can append the_ID
value 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.
如果仅仅查询一条记录,可以通过把_ID的值加到URI里去查询。
content://. . . ./23 (查询ID = 23的那条数据)
There are some helper methods, particularly
and ContentUris.withAppendedId()
, that make it easy to append an ID to a URI. Both are static methods that return a Uri object with the ID added. Uri.withAppendedPath()
这有一些方法,例如ContentUris.withAppendedId() 和 Uri.withAppendedPath().这两个方法都可以帮助我们把ID加到URI后面,这两个static的方法都会返回一个增加了ID得URI对象。
//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);
Declaring the content provider
The name
attribute is the fully qualified name of the ContentProvider subclass. Theauthorities
attribute is the authority part of thecontent:
URI that identifies the provider. For example if the ContentProvider subclass is AutoInfoProvider,
name这个属性是ContentProvider这个子类的全名,也就是继承ContentProvider这个类的名字,authorities这个属性是content这个URI的一部分。
<provider android:name="com.example.autos.AutoInfoProvider"
android:authorities="com.example.autos.autoinfoprovider"></provider>
Other<provider>
attributes can set permissions to read and write data, provide for an icon and text that can be displayed to users, enable and disable the provider, and so on. Set themultiprocess
attribute to "true
" if data does not need to be synchronized between multiple running versions of the content provider. This permits an instance of the provider to be created in each client process, eliminating the need to perform IPC.
其他provider 属性可以设置,读写权限,是否提供图片和icon的展示权限,provider的 enable,disable 权限等等,设置multipprocess这个属性,为 true,如果数据不需要在多个运行的版本间同步,那么就可以在每个进程中实例一个ContentProvider
对于ContentProvider 文档中的部分内容的分析,本人新手一枚,希望和大家多多交流多多探讨。