安卓学习 之 ContentResolver内容提供者(七)
生活随笔
收集整理的這篇文章主要介紹了
安卓学习 之 ContentResolver内容提供者(七)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡介
Android持久化技術一章中所保存的數(shù)據(jù)都只能在當前應用程序中訪問,但跨程序數(shù)據(jù)共享是由Content Provider提供的,譬如說:電話薄、短信、媒體庫中的信息。
一、基本用法
| uri | from table_name | 指定查詢某個應用程序下的某一張表 |
| projection | select column1, column2 | 指定查詢的列名 |
| selection | where column = value | 指定 where 的約束條件 |
| selectionArgs | - | 為 where 中的占位符提供具體的值 |
| orderBy | order by column1, column2 | 指定查詢結果的排序方式 |
應用(讀取系統(tǒng)聯(lián)系人)
private void readContacts() { Cursor cursor = null;try {// 查詢聯(lián)系人數(shù)據(jù)cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);while (cursor.moveToNext()) {// 獲取聯(lián)系人姓名String displayName = cursor.getString(cursor.getColumnIndex( ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// 獲取聯(lián)系人手機號String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));contactsList.add(displayName + "\n" + number);}} catch (Exception e) { e.printStackTrace();} finally {if (cursor != null) { cursor.close();} }然后設置ArrayAdapter把數(shù)據(jù)渲染到ListView中,這里就不展示了。
二、創(chuàng)建內(nèi)容提供者
1.新建類繼承自ContentProvider類,該類中有6個抽象方法,復寫六個方法。
- OnCreate():初始化內(nèi)容提供器、完成數(shù)據(jù)庫創(chuàng)建和升級等,存在Content Resolver時調(diào)用;
- query():在內(nèi)容提供器中查詢數(shù)據(jù),使用uri參數(shù)來確定是那張表,每個參數(shù)代表不同意思;
- insert()項內(nèi)容提供器添加一條數(shù)據(jù),根據(jù)uri和values來完成添加;
- update()更新數(shù)據(jù);
- delete()刪除數(shù)據(jù);
- getType()根據(jù)傳入URI來返回相應的MIME類型。
URI具體可以這樣寫:
2.借助URIMatcher來實現(xiàn)匹配內(nèi)容URI的功能,而且URIMatcher提供了一個addURI方法,并將authority、path和自定義代碼傳入。
static {uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI("com.example.app.provider", "table1", TABLE1_DIR); uriMatcher.addURI("com.example.app.provider ", "table1/#", TABLE1_ITEM); uriMatcher.addURI("com.example.app.provider ", "table2", TABLE2_ITEM); uriMatcher.addURI("com.example.app.provider ", "table2/#", TABLE2_ITEM); }3.復寫query方法,利用UriMathcer的match()方法對傳入URI進行匹配,得到相應的數(shù)據(jù)。UriMathcer的match()的輸出是2步的自定義代碼,代表的是表中的哪一列。
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {switch (uriMatcher.match(uri)) { case TABLE1_DIR:// 查詢table1表中的所有數(shù)據(jù)break;case TABLE1_ITEM:// 查詢table1表中的單條數(shù)據(jù)break;case TABLE2_DIR:// 查詢table2表中的所有數(shù)據(jù)break;case TABLE2_ITEM:// 查詢table2表中的單條數(shù)據(jù)break; default:break;} }4.getType()方法用于獲取Uri對象所對應的MIME類型。它是所有的內(nèi)容提供器都必須提供的一個方法,用于獲取 Uri 對象所對應的 MIME 類型。MIME格式如下:
應用(創(chuàng)建Book和Category的內(nèi)容提供者)
public class DatabaseProvider extends ContentProvider {public static final int BOOK_DIR = 0;public static final int BOOK_ITEM = 1;public static final int CATEGORY_DIR = 2;public static final int CATEGORY_ITEM = 3;public static final String AUTHORITY = "com.example.databasetest.provider";private static UriMatcher uriMatcher;private MyDatabaseHelper dbHelper;static {uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);uriMatcher.addURI(AUTHORITY, "book", BOOK_DIR);uriMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);uriMatcher.addURI(AUTHORITY, "category", CATEGORY_DIR);uriMatcher.addURI(AUTHORITY, "category/#", CATEGORY_ITEM);}@Overridepublic boolean onCreate() {dbHelper = new MyDatabaseHelper(getContext(), "BookStore.db", null, 2);return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {// 查詢數(shù)據(jù)SQLiteDatabase db = dbHelper.getReadableDatabase();Cursor cursor = null;switch (uriMatcher.match(uri)) {case BOOK_DIR:cursor = db.query("Book", projection, selection, selectionArgs, null, null, sortOrder);break;case BOOK_ITEM:String bookId = uri.getPathSegments().get(1);cursor = db.query("Book", projection, "id = ?", new String[] { bookId }, null, null, sortOrder);break;case CATEGORY_DIR:cursor = db.query("Category", projection, selection, selectionArgs, null, null, sortOrder);break;case CATEGORY_ITEM:String categoryId = uri.getPathSegments().get(1);cursor = db.query("Category", projection, "id = ?", new String[] { categoryId }, null, null, sortOrder);break;default:break;}return cursor;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// 添加數(shù)據(jù)SQLiteDatabase db = dbHelper.getWritableDatabase();Uri uriReturn = null;switch (uriMatcher.match(uri)) {case BOOK_DIR:case BOOK_ITEM:long newBookId = db.insert("Book", null, values);uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + newBookId);break;case CATEGORY_DIR:case CATEGORY_ITEM:long newCategoryId = db.insert("Category", null, values);uriReturn = Uri.parse("content://" + AUTHORITY + "/category/" + newCategoryId);break;default:break;}return uriReturn;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {// 更新數(shù)據(jù)SQLiteDatabase db = dbHelper.getWritableDatabase();int updatedRows = 0;switch (uriMatcher.match(uri)) {case BOOK_DIR:updatedRows = db.update("Book", values, selection, selectionArgs);break;case BOOK_ITEM:String bookId = uri.getPathSegments().get(1);updatedRows = db.update("Book", values, "id = ?", new String[] { bookId });break;case CATEGORY_DIR:updatedRows = db.update("Category", values, selection, selectionArgs);break;case CATEGORY_ITEM:String categoryId = uri.getPathSegments().get(1);updatedRows = db.update("Category", values, "id = ?", new String[] { categoryId });break;default:break;}return updatedRows;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// 刪除數(shù)據(jù)SQLiteDatabase db = dbHelper.getWritableDatabase();int deletedRows = 0;switch (uriMatcher.match(uri)) {case BOOK_DIR:deletedRows = db.delete("Book", selection, selectionArgs);break;case BOOK_ITEM:String bookId = uri.getPathSegments().get(1);deletedRows = db.delete("Book", "id = ?", new String[] { bookId });break;case CATEGORY_DIR:deletedRows = db.delete("Category", selection, selectionArgs);break;case CATEGORY_ITEM:String categoryId = uri.getPathSegments().get(1);deletedRows = db.delete("Category", "id = ?", new String[] { categoryId });break;default:break;}return deletedRows;}@Overridepublic String getType(Uri uri) {switch (uriMatcher.match(uri)) {case BOOK_DIR:return "vnd.android.cursor.dir/vnd.com.example.databasetest. provider.book";case BOOK_ITEM:return "vnd.android.cursor.item/vnd.com.example.databasetest. provider.book";case CATEGORY_DIR:return "vnd.android.cursor.dir/vnd.com.example.databasetest. provider.category";case CATEGORY_ITEM:return "vnd.android.cursor.item/vnd.com.example.databasetest. provider.category";}return null;}}總結
以上是生活随笔為你收集整理的安卓学习 之 ContentResolver内容提供者(七)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓学习 之 数据存储(六)
- 下一篇: 安卓学习 之 多媒体技术(八)