1.概述
ContentProvider爲存儲和讀取數據提供了統一的接口,實現了程序間的數據共享,而應用程序內部沒有必要實現這個功能,直接操作數據庫就可以!Android內置的許多數據都是使用ContentProvider形式,供開發者調用的(如視頻,音頻,圖片,通訊錄等)。
當應用繼承ContentProvider類,並重寫該類用於提供數據和存儲數據的方法,就可以向其他應用共享其數據,統一了數據訪問方式。
2.提供一個ContentProvider
/* * PersonProvider.java * 內容提供者 */ public class PersonProvider extends ContentProvider { private SQLiteDatabase db; //需要操作的數據庫 private final static int PERSON = 1; private final static int PERSONID = 2; private final static int DELPERSONID = 3; private final static int PERSONS = 4; // 1.定義一個靜態常量字段Uri匹配器,用以匹配uri private final static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); //如果都匹配不成功,則返回構造函數中指定的參數值,默認爲-1 // 2.靜態代碼塊 static{ /* * Add a URI to match, and the code to return when this URI is matched. * 使用"*"去匹配任意的文本數據,或者使用"#"去匹配任意的數字 * MATCHER.addURI(String authority, // 主機名或者認證 com.baidu.provider.person * String path, // 路徑 authority/path * int code) // 與這個authority的path路徑想對應的,一個int的數字,使用MATCHER.match(uri) 返回code數字 */ MATCHER.addURI("com.baidu.provider.person", "person", PERSON); // MATCHER.addURI("com.baidu.provider.person", "persons", PERSONS); MATCHER.addURI("com.baidu.provider.person", "person/update/#", PERSONID); MATCHER.addURI("com.baidu.provider.person", "person/delete/#", DELPERSONID); } // 3.onCreate方法完成provider的初始化動作,主要是數據庫的構建 @Override public boolean onCreate() { DbOpenHelper dbOpenHelper = new DbOpenHelper(this.getContext()); db = dbOpenHelper.getWritableDatabase(); return true; } /* * 4.覆寫需要的方法 * query,insert,delete,update */ @Override public Uri insert(Uri uri, ContentValues values) { //4.1 使用uri匹配器去匹配一個uri,根據int返回值來判斷具體的操作類型 switch (MATCHER.match(uri)) { case PERSON: //4.2與uri想匹配的響應操作 long id = db.insert("person", "name", values); //返回id uri = ContentUris.withAppendedId(uri, id); //返回需求的uri break; default: break; } return uri; } }
<!-- AndroidManifest.xml --> <provider android:name=".PersonProvider" android:authorities="com.baidu.provider.person"></provider>
3.ContentProvider的增刪改查
(1).增加操作
/* * ContentProvider 增操作 */ // 提供者內部方法 public Uri insert(Uri uri, ContentValues values) { switch (MATCHER.match(uri)) { case PERSON: long id = db.insert("person", "name", values); uri = ContentUris.withAppendedId(uri, id); //注意使用這個簡便的操作方法,來生成一個uri break; default: break; } return uri; } // 使用提供者的內部方法 public void insertMethod(){ ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver(); Uri uri = Uri.parse("content://com.baidu.provider.person/person"); List<String> list = uri.getPathSegments(); ContentValues values = new ContentValues(); values.put("name", "zhang"); values.put("age", "20"); Uri resultUri = resolver.insert(uri, values); if(ContentUris.parseId(resultUri) > 0){ //根據uri,解析出其中的id值 show("插入成功!"); }else{ show("插入失敗!"); } } public void show(String info){ Toast.makeText(this, info, Toast.LENGTH_SHORT).show(); } </span></strong>
(2).刪除操作
/* * ContentProvider 刪操作 */ // 提供者內部方法 public int delete(Uri uri, String selection, String[] selectionArgs) { int resultId = 0; switch (MATCHER.match(uri)) { case DELPERSONID: long id = ContentUris.parseId(uri); //根據uri對象,解析出其中的id,解析失敗返回爲-1 if(selection != null){ selection += (" and _id="+id); }else{ selection = ("_id="+id); } resultId = db.delete("person", selection, selectionArgs); //返回刪除成功的行號值 break; } return resultId; } //使用提供者的內部方法 public void deleteMethod(){ ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver(); Uri uri = Uri.parse("content://com.baidu.provider.person/person/delete/6"); int id = resolver.delete(uri, null, null); if(id > 0){ show("刪除成功!"); }else{ show("刪除失敗!"); } } public void show(String info){ Toast.makeText(this, info, Toast.LENGTH_SHORT).show(); }
(3).更新操作
/* * ContentProvider 更新操作 */ // 提供者內部方法 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int resultId = 0; switch (MATCHER.match(uri)) { case PERSONID: long id = ContentUris.parseId(uri); if(selection != null){ selection += (" and _id="+id); }else{ selection = ("_id="+id); } resultId = db.update("person", values, selection, selectionArgs); break; default: break; } return resultId; } //使用提供者的內部方法 public void updateMethod(){ ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver(); Uri uri = Uri.parse("content://com.baidu.provider.person/person/update/5"); ContentValues values = new ContentValues(); values.put("name", "li"); values.put("age", "24"); int id = resolver.update(uri, values, null, null); if(id > 0){ show("更新成功!"); }else{ show("更新失敗!"); } } public void show(String info){ Toast.makeText(this, info, Toast.LENGTH_SHORT).show(); }
(4).查詢操作
/* * ContentProvider 查詢操作 */ // 提供者內部方法 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = null; switch (MATCHER.match(uri)) { case PERSONS: cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); break; default: break; } return cursor; } //使用提供者的內部方法 ContentResolver resolver = MainActivity.this.getApplicationContext().getContentResolver(); Uri uri = Uri.parse("content://com.baidu.provider.persons"); Cursor cursor = resolver.query(uri, null, null, null, null); if(cursor != null){ list.setAdapter(new SimpleCursorAdapter(MainActivity.this, R.layout.item, cursor, new String[]{"_id", "name", "age"}, new int[]{R.id.id, R.id.name, R.id.age})); show("查詢成功!"); }else{ show("查詢失敗!"); } public void show(String info){ Toast.makeText(this, info, Toast.LENGTH_SHORT).show(); }
4.內容觀察者
在listView中,使用內容提供者動態更新數據ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://sms/"); resolver.registerContentObserver(uri, true, new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); /* * 發送一個Message對象 * 有handler對對象進行處理 */ } });
5.操作示意圖