Content Provider

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 insertion
Uri mNewUri;

...

// Defines an object to contain the new values to insert
ContentValues 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 values
ContentValues mUpdateValues = new ContentValues();

// Defines selection criteria for the rows you want to update
String mSelectionClause = UserDictionary.Words.LOCALE +  "LIKE ?";
String[] mSelectionArgs = {"en_%"};

// Defines a variable to contain the number of updated rows
int 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 delete
String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?";
String[] mSelectionArgs = {"user"};

// Defines a variable to contain the number of rows deleted
int mRowsDeleted = 0;

...

// Deletes the words that match the selection criteria
mRowsDeleted = 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值