~~~~~~~~~~SDK之ContentProvider學習筆記~~~~~~~~~~~
ContentProvider的作用如下:
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.
-------如果要自訂一個content provider,最好為它的uri定義一個常量,這樣就方便用戶端的代碼以及後期的更新。每個content provider都會開放一個公用的URI(由一個Uri對象封裝),這個URI唯一的標識了content provider的資料集。所有content provider的URI都是以字串“content://"開始的。
URI常量使用在與content provider的所有互動中。
-------查詢content provider:
查詢一個content provider需要三方面的資訊:
(1)標識provider的URI
(2)準備接受的資料域的名稱
(3)這些資料域的資料類型
如果是要尋找某個特定的記錄,那麼還需要知道那條記錄的ID。
Making a query:
可以使用ContentResolver.query()或Activity.managerdQuery()方法。
上述兩個查詢函數的第一個參數都是content provider的uri。
如果想要將查詢限制在某一條記錄,可以在URI的後面添加那條記錄的_ID值。可以通過 ContentUris.withAppendedId() 和Uri.withAppendedPath()來實現
對於查詢返回的Cursor對象,只能使用它進行資料的讀取,至於資料的增加、刪除和修改同必須通過ContentSolver對象來實現。
Reading retrieved data:
Cursor對象提供了一些方法用於讀取查詢到的記錄如:getString()/getInt()/getFloat(),但是這些方法的參數都是對應列的index,所以首先需要通過列名稱來獲得對應的index,才能讀取資料。而Cursor對象本身就支援從列名稱獲得index,從index獲得列名稱的方法。如:Cursor.getColumnIndex().
modifying data:
由ContentProvider儲存的資料可以在下面的幾個方面進行修改:
(1)Adding new records
(2)Adding new values to existing records
(3)Batch updating existing records
(4)Deleting records
所有資料的修改都要通過ContentResolver的方法來實現。同時也要注意事先獲得資料的修改許可權,否則會修改失敗。
(1)adding records:使用ContentValues對象和ContentResolver.insert()方法。
如果是向ContentProvider中存放圖片、聲音等大容量的多媒體資訊時,一般都是在provider中暫時存放它們的uri而已,而開啟的時候是通過ContentResolver.openOutputStream()方法實現的。
(2)adding new values:使用ContentResolver.insert()返回的uri做進一步的處理。
(3)batch updating records:使用 ContentResolver.update() 方法更新列的值。
(4)deleting a record:使用ContentResolver.delete()
注意:android虛擬機器中的應用程式的私人資料一般都是存放在/data/data/{包名}/databases下面的。如:contacts應用程式在/data/data/com.android.providers.contacts/databases/目錄下,就存在了contacts2.db的SQLite的資料庫檔案。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如何建立ContentProvider?
主要包括如下的幾個方面:
(1)Set up a system for storing the data.
注意: Android provides the SQLiteOpenHelper class to help you create a database and SQLiteDatabase to manage it.
(2)Extend the ContentProvider class to provide access to the data.
注意:需要實現6個成員方法
query()
insert()
update()
delete()
getType()
onCreate():當provider被啟動的時候調用。
這些方法都可能被ContentResolver對象所調用。
call ContentResolver.notifyChange() to notify listeners when there are modifications to the data.
定義ContentProvider的幾個步驟:
----Define a public static final Uri named CONTENT_URI. This is the string that represents the full content: URI that your content provider handles.
--------Define the column names that the content provider will return to clients.
--------Carefully document the data type of each column. Clients need this information to read the data.
--------If you are handling a new data type, you must define a new MIME type to return in your implementation of ContentProvider.getType().
(3)Declare the content provider in the manifest file for your application(AndroidManifest.xml).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~··
學習小結:
提示:對於自訂的ContentProvider,由於其他的其他的應用程式很可能會使用其中的一些待用資料,所以可以將它匯出為JAR歸檔檔案。
開發Android應用程式時,一定要注意是否在AndroidManifext.xml檔案中是否加入合適的存取權限,否則會導致莫名其妙的錯誤提示。
看完Android SDK中的ContentProvider中的內容之後:
Cursor、ContentProvider、ContentResolver三者之間的關係應該是這樣的:
ContentProvider主要負責的是資料的儲存和提供底層的資料維護操作,它是不和客戶代碼直接發生聯絡的。
ContentResolver主要負責用戶端代碼的工作,是直接響應UI介面的操作的,通過它來調用ContentProvider中的一些回呼函數實現資料的增、刪、改、查。
Cursor主要是顯示從ContentProvider中擷取的資料,它本身只能提供顯示讀取的功能,不能進行修改。
在SQLite實現資料維護操作,一般都是通過SQLiteOpenHelper的繼承子類獲得SQLiteDatabase對象執行個體,再來實現資料操作,這樣比較方便。
在實際的編程開發中,一般不會直接使用Cursor的子類對象的操作來顯示資料,而是通過將Cursor子類對象與適配器空間聯絡起來,進而實現資料的顯示。