天天看點

Android四大元件之——ContentProvider(一)

目錄

<a href="http://www.cnblogs.com/johntsai/p/4034062.html#1">1.contentprovider</a>

<a href="http://www.cnblogs.com/johntsai/p/4034062.html#2">2.content uri</a>

相信大家都知道大名鼎鼎的contentprovider,作為android四大元件之一,和activity、service、broadcastreceiver齊名。

今天就和大家一起來學習一下contentprovider。

android開發者文檔這麼說的:

content providers manage access to a structured set of data. they encapsulate the data, and provide mechanisms for defining data security.

content providers管理對有結構的資料集的通路。它們封裝資料,提供了定義資料安全的機制。

這個說法似乎太官方了,不好了解。究竟contentprovider是怎樣管理,封裝資料呢?又是怎樣保證資料安全的?

通俗的說,contentprovider是一種資料包裝器。它提供統一的接口對資料進行操作,使用者不用關心資料到底是如何存儲的以及資料類型到底是什麼。contentprovider将資料封裝好了,這樣就能友善的管理資料了。它主要用于不同應用間,不同程序間的資訊的共享。

Android四大元件之——ContentProvider(一)

大家都知道,涉及到資料的通路就要考慮到資料的安全性。怎樣在保證資料的安全性的同時,又能友善的通路資料呢?

衆所周知,android系統是基于linux核心的。在linux中,檔案具有如下圖中的一系列屬性,如檔案權限。不同的使用者或使用者組對檔案有不同的讀寫,執行的權限。如圖中的r(read),w(write),x(execute),-(無權限)。

Android四大元件之——ContentProvider(一)

android是基于linux的,也繼承了linux的檔案管理方式,通常每個應用都是獨立的程序,也就是不同的使用者。android為每個應用程式配置設定了獨立的使用者id和使用者組id。并且由這個應用程式建立出來的檔案被賦予了相應的讀寫權限。其他應用程式無權通路。

Android四大元件之——ContentProvider(一)

這樣就保證了資料的安全性,但是這對資料的共享給第三方造成了不便。android系統的開發者為了解決了這個問題,設計了contentprovider類。content provider很好的兼顧了二者。

Android四大元件之——ContentProvider(一)

在了解content uri之前,先了解下uri.

uri:通用資源辨別符(uniform resource identifier, 簡稱"uri")主要用于web上可用的每種資源 -html文檔、圖像、視訊片段、程式等 - 進行定位。

android上的資源也可以用uri表示。

Android四大元件之——ContentProvider(一)

文檔中是這麼描述uri的:

a content uri is a uri that identifies data in a provider. content uris include the symbolic name entire provider (its authority) and a name that points to a table (a path).

例如:

使用者字典的單詞表:

<code>content://user_dictionary/word</code>

裝置上存儲的所有圖檔:

<code>content://media/internal/images</code>

聯系人資訊:

<code>content://contacts/people</code>

某個人的聯系人資訊:

<code>content://contacts/people/1</code>

使用uri的parse方法将字元串轉換為uri:

<code>&lt;uri uri = uri.parse("content://com.ijtsai.contactprovider/people");&gt;</code>

繼續閱讀