Android:使用ContentProvider共享数据

    xiaoxiao2021-12-01  63

    一、在AndroidManifest.xml中加入: <provider     android:name=".TalkHistoryProvider" android:authorities="com.g.im.talkhistoryprovider" /> 二、自定义ContentProvider类: public class TalkHistoryProvider extends ContentProvider { private static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int TALKHISTORYS = 1; // 集合类型 private static final int TALKHISTORY = 2; // 单个类型 private DBOpenHelper mDBOpenHelper; static { mUriMatcher.addURI("com.g.im.talkhistoryprovider", "talkhistory", TALKHISTORYS); mUriMatcher.addURI("com.g.im.talkhistoryprovider", "talkhistory/#", TALKHISTORY); } @Override public boolean onCreate() { mDBOpenHelper = DBOpenHelper.getDBOpenHelper(this.getContext().getApplicationContext()); return true; } @Override public String getType(Uri uri) { switch (mUriMatcher.match(uri)) { case TALKHISTORYS: return "vnd.android.cursor.dir/talkhistory"; // 返回集合类型 case TALKHISTORY: return "vnd.android.cursor.item/talkhistory"; // 返回单个类型 default: throw new IllegalArgumentException("Unkown Uri:" + uri); } } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mDBOpenHelper.getWritableDatabase(); long id = 0; switch (mUriMatcher.match(uri)) { case TALKHISTORYS: id = db.insert("talkhistory", "user_id", values); // 将数据插入DB中,当values数据为空时,给user_id赋值NULL,返回记录的id getContext().getContentResolver().notifyChange(uri, null); // 通知外部数据被改变了 return ContentUris.withAppendedId(uri, id); // 返回代表新增记录的Uri case TALKHISTORY: id = db.insert("talkhistory", "user_id", values); String uri1 = uri.toString(); Uri talkhistoryUri = Uri.parse(uri1.substring(0, uri1.lastIndexOf("/"))); getContext().getContentResolver().notifyChange(talkhistoryUri, null); return ContentUris.withAppendedId(talkhistoryUri, id); // 返回代表新增记录的Uri default: throw new IllegalArgumentException("Unkown Uri:" + uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = mDBOpenHelper.getWritableDatabase(); int num = 0; // 已删除的条数 switch (mUriMatcher.match(uri)) { case TALKHISTORYS: num = db.delete("talkhistory", selection, selectionArgs); break; case TALKHISTORY: long id = ContentUris.parseId(uri); String where = "user_id=" + id; if (selection != null && !"".equals(selection)) { where = where + " and " + selection; // 如user_id=1 and ... } num = db.delete("talkhistory", where, selectionArgs); break; default: throw new IllegalArgumentException("Unkown Uri:" + uri); } getContext().getContentResolver().notifyChange(uri, null); return num; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = mDBOpenHelper.getWritableDatabase(); int num = 0; // 已修改的条数 switch (mUriMatcher.match(uri)) { case TALKHISTORYS: num = db.update("talkhistory", values, selection, selectionArgs); break; case TALKHISTORY: long id = ContentUris.parseId(uri); String where = "user_id=" + id; if (selection != null && !"".equals(selection)) { where = where + " and " + selection; } num = db.update("talkhistory", values, where, selectionArgs); break; default: throw new IllegalArgumentException("Unkown Uri:" + uri); } getContext().getContentResolver().notifyChange(uri, null); // 通知外部数据被改变了 return num; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mDBOpenHelper.getReadableDatabase(); switch (mUriMatcher.match(uri)) { case TALKHISTORYS: return db.query("talkhistory", projection, selection, selectionArgs, null, null, sortOrder); case TALKHISTORY: long id = ContentUris.parseId(uri); String where = "user_id=" + id; if (selection != null && !"".equals(selection)) { where = where + " and " + selection; } return db.query("talkhistory", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("Unkown Uri:" + uri); } } } 三、使用ContentResolver对ContentProvider中的数据进行增删改查: ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.g.im.talkhistoryprovider/talkhistory"); // 增加记录 ContentValues insertValues = new ContentValues(); insertValues.put("name", "姓名1"); insertValues.put("age", 25); resolver.insert(uri, insertValues); // 删除记录 Uri deleteUri = ContentUris.withAppendedId(uri, 1); //1为id值 resolver.delete(deleteUri, null, null); // 修改记录 ContentValues updateValues = new ContentValues(); updateValues.put("name", "姓名2"); Uri updateUri = ContentUris.withAppendedId(uri, 1); //1为id值 resolver.update(updateUri, updateValues, null, null); // 查找记录 Cursor cursor = resolver.query(uri, null, null, null, "talkhistory desc"); while (cursor.moveToNext()) { long userId = cursor.getInt(0); String name = cursor.getString(1); } 四、使用ContentObserver对数据的更改进行监听: //true表示以如下Uri开头就触发onChange,false要完全一致才触发 getContentResolver().registerContentObserver(Uri.parse("content://com.g.im.talkhistoryprovider/talkhistory"), true, new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); //处理一些逻辑 } });
    转载请注明原文地址: https://ju.6miu.com/read-679524.html

    最新回复(0)