说明:文章仅供本人学习记录所用。
1.理解含义:
ContentProvider: 内容提供者,将数据以表的形式进行操作。主要实现应用程序间数据共享,操作系统本地数据(包括短 信、音频、视屏、数据库)。
ContentObserver:内容观察者,监听数据变化。
2.使用方法:
ContentProvider:
1)步骤:新建MyProvider类继承ContentProvider类;注册URI;重写方法(onCreate、query、bulkInsert、insert、 delete、update);在AndroidManifest.xml中配置provider;
2)代码:
public class MyProvider extends ContentProvider { private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int INSERT = 1; private static final int UPDATE = 2; private static final int DELETE = 3; private static final int QUERY = 4; private static final int INSERTS = 5; private DBHelper dbOpenHelper; private String TAG = "Provider"; static { // 为UriMatcher注册si四个Uri matcher.addURI(Picture.AUTHORITY, "insert", INSERT); matcher.addURI(Picture.AUTHORITY, "update", UPDATE); matcher.addURI(Picture.AUTHORITY, "delete", DELETE); matcher.addURI(Picture.AUTHORITY, "query", QUERY); matcher.addURI(Picture.AUTHORITY, "inserts", INSERTS); //说明Picture.AUTHORITY:表示你要操作的ContentProvider,一般以要操作的实体类的包名命名。 } @Override public boolean onCreate() { //获取数据库辅助类 dbOpenHelper = DBHelper.getInstance(getContext()); DatabaseManager.initializeInstance(dbOpenHelper); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub int code = matcher.match(uri); Cursor c = null; switch (code) { case QUERY: SQLiteDatabase database = dbOpenHelper.getReadableDatabase(); c = database.query("picture", projection, selection, selectionArgs, null, null, sortOrder); c.setNotificationUri(getContext().getContentResolver(), uri); DatabaseManager.getInstance().closeDatabase(); case UriMatcher.NO_MATCH: Log.i(TAG, "------------未知的URI" + uri); break; } return c; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { int code = matcher.match(uri); switch (code) { case INSERT: SQLiteDatabase database = DatabaseManager.getInstance() .openDatabase();// 线程安全并发操作数据库 try {// 获得数据库实例 database.insert("picture", null, values); getContext().getContentResolver().notifyChange(uri, null); } catch (Exception ex) { Log.i(TAG, "insert ex=====" + ex.toString()); } finally { DatabaseManager.getInstance().closeDatabase(); } break; case UriMatcher.NO_MATCH: Log.i(TAG, "------------未知的URI" + uri); break; } return null; } /** * 批量插入 */ @Override public int bulkInsert(Uri uri, ContentValues[] values) { int code = matcher.match(uri); switch (code) { case INSERTS: SQLiteDatabase database = DatabaseManager.getInstance() .openDatabase();// 线程安全并发操作数据库 try {// 获得数据库实例 int numValues = 0; numValues = values.length; for (int i = 0; i < numValues; i++) { ContentValues value = values[i]; database.delete("picture", "name = ?", new String[] { String.valueOf(value.get("name")) }); Log.i(TAG, "name===== values" + String.valueOf(value.get("name"))); database.insert("picture", null, values[i]); Log.i(TAG, "Value insert====" + i); } getContext().getContentResolver().notifyChange(uri, null); } catch (Exception ex) { Log.i(TAG, "insert ex=====" + ex.toString()); } finally { DatabaseManager.getInstance().closeDatabase(); } break; case UriMatcher.NO_MATCH: Log.i(TAG, "------------未知的URI" + uri); break; } return 0; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int code = matcher.match(uri); switch (code) { case DELETE: SQLiteDatabase database = DatabaseManager.getInstance() .openDatabase(); int i = 0; try { i = database.delete("picture", selection, selectionArgs); if (i > 0) { getContext().getContentResolver().notifyChange(uri, null); } } catch (Exception ex) { Log.i(TAG, "delete ex====" + ex.toString()); } finally { // database.close(); DatabaseManager.getInstance().closeDatabase(); } return i; case UriMatcher.NO_MATCH: Log.i(TAG, "------------未知的URI" + uri); break; } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int code = matcher.match(uri); switch (code) { case UPDATE: // SQLiteDatabase database = dbOpenHelper.getReadableDatabase(); SQLiteDatabase database = DatabaseManager.getInstance() .openDatabase(); int i = 0; try { i = database .update("picture", values, selection, selectionArgs); if (i != 0) { getContext().getContentResolver().notifyChange(uri, null); } } catch (Exception ex) { Log.i(TAG, "update ex===" + ex.toString()); } finally { // database.close(); DatabaseManager.getInstance().closeDatabase(); } return i; case UriMatcher.NO_MATCH: Log.i(TAG, "------------未知的URI" + uri); break; } return 0; } } //在数据库操作类中添加以下方法。 //单条数据添加 public static void addPicture(Picture picture) { Uri uri = Uri.parse("content://" + Picture.AUTHORITY + "/insert"); ContentValues values = new ContentValues();//封装数据 values.put("name", picture.name); ... AppApplication.getInstance().getContentResolver().insert(uri, values); } //多条数据添加 public static void addPictures(List<Picture> pictures) { Uri uri = Uri.parse("content://" + Picture.AUTHORITY + "/inserts"); ContentValues[] values = new ContentValues[pictures.size()]; for (int i = 0; i < pictures.size(); i++) { Picture picture = pictures.get(i); ContentValues value = new ContentValues(); value.put("name", picture.name); ... values[i] = value; } Log.i(TAG, "addPictures_values_size=====" + values.length); AppApplication.getInstance().getContentResolver().bulkInsert(uri, values); } //删除操作 public static void deletePictureTag() { Uri uri = Uri.parse("content://" + Picture.AUTHORITY + "/delete"); int i = AppApplication.getInstance().getContentResolver() .delete(uri, "whereName = ?", new String[] { "value" }); } //修改操作 public static void updatePicture(Picture picture) { Uri uri = Uri.parse("content://" + Picture.AUTHORITY + "/update"); ContentValues values = new ContentValues(); values.put("tagStatus", picture.tagStatus); int i = AppApplication.getInstance().getContentResolver() .update(uri, values, "whereName = ?", new String[] { picture.name }); } //AndroidManifest.xml中配置provider <provider android:name="com....provider.MyProvider"//包名 android:authorities="com....provider.myProvide" />
ContentObserver:
1)步骤:注册与注销ContentObserver;添加ContentObserver方法。
2)代码:
//uri与provider中注册的uri对应 Uri uri = Uri.parse("content://" + Picture.AUTHORITY + "/insert");//监听添加 getContentResolver().registerContentObserver(uri, true, cob); Uri uri_inserts = Uri.parse("content://" + Picture.AUTHORITY+ "/inserts");//监听批量添加 getContentResolver().registerContentObserver(uri_inserts, true, cob); Uri uri_update = Uri.parse("content://" + Picture.AUTHORITY + "/update");//监听修改 getContentResolver().registerContentObserver(uri_update, true, cob); getContentResolver().unregisterContentObserver(cob);//在onDestroy销毁 //监听数据改变的方法 private ContentObserver cob = new ContentObserver(new Handler()) { @Override public boolean deliverSelfNotifications() { return super.deliverSelfNotifications(); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); //改变数据时做出相应的操作 ...... } }