Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

四大元件之 ContentProvider(二):輕鬆自訂你的 ContentProvider

編輯:關於 Android 程式設計

第 3 節 自訂 ContentProvider

自訂一個 ContentProvider,需要繼承 ContentProvider 類別並建立一個新類別,實作其中的一些方法;同時,在應用的 AndroidManifest.xml 檔案中聲明這個新加入的元件。但在這之前,我們要先設計外部存取它所採用的位址。

3.1 位址設計

Uri 的 scheme 欄位是固定的,使用 content:。

authority 定義成程式的套件名稱 com.anddle.mycontentprovider。

path 就像網站內部的分類,依據網站的邏輯進行劃分。
假設我們的 ContentProvider 提供書籍 book 與檔案 file 兩種內容的查詢操作,而每種類型都可以進行單一資料與多筆資料的操作。

例如,

操作所有書的資訊:content://com.anddle.mycontentprovider/books; 操作某本特定書的資訊:content://com.anddle.mycontentprovider/books/8 操作所有檔案的資訊:content://com.anddle.mycontentprovider/files 操作某個特定檔案的資訊:content://com.anddle.mycontentprovider/files/3

因此,在對這些位址所代表的物件進行增刪改查時,就需要分析出它們針對的物件。

3.2 建立 ContentProvider 子類

繼承 ContentProvider 類別時,會要求我們實作 getType()、insert()、delete()、update()、query()、onCreate() 等介面:

 public class MyContentProvider extends ContentProvider { @Override public boolean onCreate() { return true; } @Override public String getType(Uri uri) { throw new UnsupportedOperationException("Not yet implemented"); } @Override public Uri insert(Uri uri, ContentValues values) { throw new UnsupportedOperationException("Not yet implemented"); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { throw new UnsupportedOperationException("Not yet implemented"); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { throw new UnsupportedOperationException("Not yet implemented"); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { throw new UnsupportedOperationException("Not yet implemented"); } }

定義提供給其他元件使用的「網路位址」URI,這裡我們把它們定義成 content://com.anddle.mycontentprovider/books 與 content://com.anddle.mycontentprovider/files:

 private static final String SCHEME = "content://"; private static final String PATH_BOOKS = "/books"; private static final String PATH_FILES = "/files"; public static final String AUTHORITY = "com.anddle.mycontentprovider"; //"content://com.anddle.mycontentprovider/books" public static final Uri CONTENT_BOOKS_URI = Uri.parse(SCHEME + AUTHORITY + PATH_BOOKS); //"content://com.anddle.mycontentprovider/files" public static final Uri CONTENT_FILES_URI = Uri.parse(SCHEME + AUTHORITY + PATH_FILES);

定義解析位址的匹配器:

 private static final int BOOKS = 0; private static final int BOOK = 1; private static final int FILES = 2; private static final int FILE = 3; private static final UriMatcher sUriMatcher; static { sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(AUTHORITY, PATH_BOOKS, BOOKS); sUriMatcher.addURI(AUTHORITY, PATH_BOOKS+"/#", BOOK); sUriMatcher.addURI(AUTHORITY, PATH_FILES, FILES); sUriMatcher.addURI(AUTHORITY, PATH_FILES+"/#", FILE); }

UriMatcher 可以對傳入的字串進行匹配檢測,如果匹配成功,會回傳一個對應的值,例如:

 int type = sUriMatcher.match("content://com.anddle.mycontentprovider/files"); //type就等於FILES的值2

type 就等於 FILES 的值 2。利用這個方法,我們就能區分出 Uri 存取 ContentProvider 時,究竟希望操作什麼樣的資料。

建立匹配器時,若加入 sUriMatcher.addURI("content://com.anddle.mycontentprovider","files/#", FILE); 這種帶有 "#" 的關鍵欄位,表示如下這種匹配方式,即匹配任何數字:

 content://com.anddle.mycontentprovider/files/0 content://com.anddle.mycontentprovider/files/1 content://com.anddle.mycontentprovider/files/3

建立匹配器時,若加入 sUriMatcher.addURI("content://com.anddle.mycontentprovider","files/*", FILE); 這種帶有 "*" 的關鍵欄位,表示如下這種匹配方式,即匹配任何字元:

 content://com.anddle.mycontentprovider/files/how-to-program content://com.anddle.mycontentprovider/files/bible content://com.anddle.mycontentprovider/files/101story

在回呼函式中,根據 Uri 做對應的操作:

 @Override public Uri insert(Uri uri, ContentValues values) { Uri result = null;? switch (sUriMatcher.match(uri)) { case BOOKS: { //從ContentValues中取出數據,保存起來;返回保存數據的Uri地址,例如 //content://com.anddle.mycontentprovider/books } break; case BOOK: { //從ContentValues中取出數據,保存起來;返回保存數據的Uri地址,例如 //content://com.anddle.mycontentprovider/books/8 } break; case FILES: { //從ContentValues中取出數據,保存起來;返回保存數據的Uri地址,例如 //content://com.anddle.mycontentprovider/files } break; case FILE: { //從ContentValues中取出數據,保存起來;返回保存數據的Uri地址,例如 //content://com.anddle.mycontentprovider/files/8 } break; default: throw new IllegalArgumentException("Unknown URI " + uri); } return result; }

這裡就要實作對資料增刪改查的真正操作。

在 ContentProvider 中,可以使用很多方式對資料進行保存、修改,例如 SQL 資料庫。不過我們暫時不去實作,把它放到下一個章節專門介紹。

其他 delete()、update()、query() 實作的函式做類似處理。

對於 getType(),需要為每一種類型的 Uri 回傳一種資料型別——MIME type,告訴呼叫者,當前這種 Uri 可以處理什麼類型的資料。

它的格式形如 type/subtype,有很多知名的 MIME type 類型,例如 application/pdf、image/jpeg 等等,可以在網上查到公開的 MIME type 有哪些,也可以自訂自己應用支援的特殊 MIME type 類型。

這裡我們就回傳一個空值:

 @Override public String getType(Uri uri) { return null; }

至此,一個 ContentProvider 就完成了,不過它現在還沒有加入真正能儲存資料的功能。

 

3.3 聲明 ContentProvider
千萬不要忘記,在應用的 AndroidManifest.xml 檔案中聲明新加入的 ContentProvider:

 

這裡的 android:authorities 屬性值,就要填寫定義 MyContentProvider 時程式碼中的那個:

 public static final String AUTHORITY = "com.anddle.mycontentprovider";

android:exported 屬性若設為 true,表示這個 ContentProvider 可以被其他應用使用(就像一個公共網站,可被任何人存取);若設為 false,表示它只能被自己所在的應用使用(就像一個內部網站,只能在公司內部存取)。

3.4 使用自訂 ContentProvider

無論是使用自己應用的 ContentProvider,還是用其他應用提供的,它們的使用方式都和使用系統提供的 ContentProvider 一樣。

新增一筆資料:透過 ContentResolver 取得存取 ContentProvider 的入口,使用 ContentValues 加入要插入的資料:

 ContentResolver cr = getContentResolver(); ContentValues cv = new ContentValues(); cv.put("數據字段名稱", "數據內容"); Uri uri = cr.insert("content://com.anddle.mycontentprovider/books", cv)

通常會回傳指向剛成功插入的這筆資料的 Uri(內容就像 content://com.anddle.mycontentprovider/books/8)。

刪除一筆資料:透過 ContentResolver 取得存取入口,使用 Uri 刪除指定的資料:

 String where = null; String [] keywords = null; ContentResolver cr = getContentResolver(); cr.delete("content://com.anddle.mycontentprovider/books/8", where, keywords);

修改一筆資料:透過 ContentResolver 取得存取入口,使用 Uri 更新指定的資料,要修改的資料放在 ContentValues 當中:

 String where = null; String [] keywords = null; ContentResolver cr = getContentResolver(); ContentValues cv = new ContentValues(); cv.put("數據字段名稱", "新數據內容",where,keywords); cr.update("content://com.anddle.mycontentprovider/books", cv, where, keywords)

查詢某一類的資料(或特定某筆資料):

 Uri uri = MyContentProvider.CONTENT_BOOKS_URI; String[] searchKey = null; String where = null; String [] keywords = null; String sortOrder = null; ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query( uri, searchKey, where, keywords, sortOrder); if(cursor != null) { while(cursor.moveToNext()) { ...... } cursor.close(); }

注意,在刪改查的操作中,還會用到諸如 where、sortOrder、keywords、searchKey 這樣的參數,它們是輔助 ContentProvider 查詢特定資料時用的。

雖然 Uri 已經能定位到某筆具體的資料,但大部分的 ContentProvider 都是透過 SQL 資料庫來實現真正的儲存,因此在設計這些介面時就保留了 SQL 語言的一些用法,讓使用者也能像直接操作 SQL 資料庫那樣靈活方便地使用 ContentProvider。

假如 ContentProvider 不用 SQL 實現資料儲存功能,而採用別的儲存機制,那麼這些額外的參數就可以派上別的用場,或者完全用不著。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved