Content Provider

来源:互联网 发布:男 休闲鞋类推荐 知乎 编辑:程序博客网 时间:2024/06/06 04:31

Content provider 管理获取结构化的数据集合。她们封装数据,并提供定义数据安全的机制。Content provider 是从这个进程获取另一个进程数据的标准接口。


当你想通过Content Provider获取数据的时候,你通过使用你程序的Context里的ContentResolver对象来和client的Provider进行。和provider对象交流的ContentProvider对象,是一个实现ContentProvider class 的实例。provider对象接收到client的请求,

执行请求的操作,并返回数据结果。


如果你不想和其他程序共享你的数据的话,你不需要开发你自己的provider。但是,如果你需要为你自己的程序提供搜索意见的话,你就要开发一个。如果你想从你的程序复制或粘贴完整的数据到其他程序的话,你也要创建一个。


Android 本身自己就有管理audio video images contacts信息的 content provider。你可以在android.provider包中看到他们。


---------------------------------------------------------------------------------------------------------------

Content Provider Basic

content provider 提供管理中央数据仓库的访问。一个content Provider是一个提供自己的UI来处理数据的一部分程序。然而,Content Provider 主要是为了个给其他程序使用,来通过provider client访问provider。同时,provider 和provider client提供一个

便利的,标准的数据访问接口,也处理进程间的访问和数据安全交流。


下面几个主题将会被讨论到:

1content providers 是如何工作的

2你从content provider接受数据的API

3你通过content  provider 插入更新和删除数据的API

4能够和providers有效工作的API

---------------------------------------

        访问一个provider(accessing a provider)

一个程序通过 content provider的一个ContentResolver client来访问数据。这个client对象中的方法和content provider中的方法名称一样,是content provider的一个实例。content provider为持久化存储提供基础的“CRUD”函数。

????

为了通过User Dictionary Provider来获取一些文字,你可以调用ContentResolver.query().query()方法调用用户Dictionary Provider的ContentProvider,query()

--------------------------------------

       Content URIs

Content URI 是一个确定provider里面数据的URI。Content URIS包括了provider的象征性的名字(authority)和一个指向表格的名称(path)。当你调用client的方法去访问provider的表格时,这个content URI是其中的一个参数。一些provider允许你通过在URI后面添加ID值来访问一个table的某一row.

note:

------------------------------------------------------------------

从Provider中接受数据(Retrieving data from the provider)


-------------------------------------------------------------------

插入、更新、删除数据 (Inserting ,Updating,Deleting Data)

-------------------------------------------------------------

inserting data

<span style="font-size:10px;">// Defines a new Uri object that receives the result of the insertionUri mNewUri;...// Defines an object to contain the new values to insertContentValues mNewValues = new ContentValues();/* * Sets the values of each column and inserts the word. The arguments to the "put" * method are "column name" and "value" */mNewValues.put(UserDictionary.Words.APP_ID, "example.user");mNewValues.put(UserDictionary.Words.LOCALE, "en_US");mNewValues.put(UserDictionary.Words.WORD, "insert");mNewValues.put(UserDictionary.Words.FREQUENCY, "100");mNewUri = getContentResolver().insert(    UserDictionary.Word.CONTENT_URI,   // the user dictionary content URI    mNewValues                          // the values to insert);</span>

return data

<span style="font-size:10px;">content://user_dictionary/words/<id_value></span>





--------------------------------------------------------------

Updating data

// Defines an object to contain the updated valuesContentValues mUpdateValues = new ContentValues();// Defines selection criteria for the rows you want to updateString mSelectionClause = UserDictionary.Words.LOCALE +  "LIKE ?";String[] mSelectionArgs = {"en_%"};// Defines a variable to contain the number of updated rowsint mRowsUpdated = 0;.../* * Sets the updated value and updates the selected words. */mUpdateValues.putNull(UserDictionary.Words.LOCALE);mRowsUpdated = getContentResolver().update(    UserDictionary.Words.CONTENT_URI,   // the user dictionary content URI    mUpdateValues                       // the columns to update    mSelectionClause                    // the column to select on    mSelectionArgs                      // the value to compare to);


---------------------------------------------------------------

deleting data

// Defines selection criteria for the rows you want to deleteString mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?";String[] mSelectionArgs = {"user"};// Defines a variable to contain the number of rows deletedint mRowsDeleted = 0;...// Deletes the words that match the selection criteriamRowsDeleted = getContentResolver().delete(    UserDictionary.Words.CONTENT_URI,   // the user dictionary content URI    mSelectionClause                    // the column to select on    mSelectionArgs                      // the value to compare to);


-------------------------------------------------------------------

Provider Data types


text、long integer、floating point、long floating point (double)、Binary Large OBject ( BLOB)通过64type的array实现。

providers 同时维护为每个content URI的MIME data type 。你可以使用MIME type的信息来发现你的程序是否能够处理provider提供的数据。或者选择一个基于MIME type的数据类型来处理。当你处理一个包含了复杂的数据结构或文件的provider时,你需要用到MIME type。举个例子,Contacts Provider中的ContactsContract.Data表使用MIME type来标签每一行contact data。通过调用ContentResolver.getType()方法,可以获取到和content type相一致的MIME type。

------------------------------------------------------------------------------------

访问Provider可选的形式  (Alternative Forms of Provider Access)

1 batch access

2 Asynchronous queries                                        CursorLoader

3 Data access via intents



------------------------------------------------------------------------------------

Contract Classes

-------------------------------------------------------------------------------------

MIME Type Reference

MIME type 格式

type/subtype


尼玛什么意思





------------------------------------------------------------------------------------------------------------

创建Content Provider

s

一个Content Provider 管理中心数据仓库的访问。你需要在你的程序中实现一个或多个的类,并在manifest节点中声明。你的类必须要实现帮助你的Provider和其他程序交流的接口的Content Provider的子类。虽然Content Provider让其他程序能够访问数据,但你也应该有activity让用户来通过你的provider来查询和修改数据。

0 0
原创粉丝点击