Content Provider
- 需要一个Authority
- Uri
- 跨进程通信
- 每次insert update..,都需要notifyChange
- 专门用于应用间数据共享,也就是跨进程通信
- 底层也是基于Binder,但使用起来方便
- 通过contentResolver的query、update、insert和delete方法即可
- 系统预置了许多contentProvider,比如通讯录、日程表信息等
- 自定义一个ContentProvider ,contentProvider需要在清单文件里面进行注册
package com.lzw.cha01.myapplication.chapter2; import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; /** * MyApplication * Created by lzw on 2019/3/11. 10:41:46 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class BookProvider extends ContentProvider { private static final String TAG = "BookProvider"; public static final String AUTHORITY = "com.lzw.cha01.myapplication.chapter2.book.provider"; public static final Uri BOOK_CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/book"); public static final Uri USER_CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/user"); public static final int BOOK_URI_CODE = 0; public static final int USER_URI_CODE =1; private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); private SQLiteDatabase mDb; private Context context; static { uriMatcher.addURI(AUTHORITY,"book",BOOK_URI_CODE); uriMatcher.addURI(AUTHORITY,"user",USER_URI_CODE); } private void initProviderData(){ mDb = new DbOpenHelper(context).getWritableDatabase(); mDb.execSQL("delete from "+ DbOpenHelper.BOOK_TABLE_NAME); mDb.execSQL("delete from "+ DbOpenHelper.USER_TABLE_NAME); mDb.execSQL("insert into " + DbOpenHelper.BOOK_TABLE_NAME +" values(3,'android');"); mDb.execSQL("insert into " + DbOpenHelper.BOOK_TABLE_NAME +" values(4,'IOS');"); mDb.execSQL("insert into " + DbOpenHelper.BOOK_TABLE_NAME +" values(5,'HTML5');"); mDb.execSQL("insert into " + DbOpenHelper.USER_TABLE_NAME +" values(1,'jake',1);"); mDb.execSQL("insert into " + DbOpenHelper.USER_TABLE_NAME +" values(2,'jasmine',0)"); } @Override public boolean onCreate() { context = getContext(); initProviderData(); return false; } @Nullable @Override public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { Log.i(TAG, "query: current thread:"+Thread.currentThread().getName()); String tbName = getTableName(uri); if (tbName == null){ throw new IllegalArgumentException("Unsupported URI:"+uri); } return mDb.query(tbName,projection,selection,selectionArgs,null,null,sortOrder,null); } @Nullable @Override public String getType(@NonNull Uri uri) { Log.i(TAG, "getType,"); return null; } @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { Log.i(TAG, "insert: "); String tbName = getTableName(uri); if (tbName == null){ throw new IllegalArgumentException("Unsupported URI:"+uri); } mDb.insert(tbName,null,values); context.getContentResolver().notifyChange(uri,null); return uri; } @Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { Log.i(TAG, "delete: "); String tbName = getTableName(uri); if (tbName == null){ throw new IllegalArgumentException("Unsupported URI:"+uri); } int count = mDb.delete(tbName,selection,selectionArgs); if (count > 0){ context.getContentResolver().notifyChange(uri,null); } return count; } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { Log.i(TAG, "update: "); String tbName = getTableName(uri); if (tbName == null){ throw new IllegalArgumentException("Unsupported URI:"+uri); } int row = mDb.update(tbName,values,selection,selectionArgs); if (row > 0){ context.getContentResolver().notifyChange(uri,null); } return row; } private String getTableName(Uri uri){ String tableName = null; switch (uriMatcher.match(uri)){ case BOOK_URI_CODE: tableName = DbOpenHelper.BOOK_TABLE_NAME; break; case USER_URI_CODE: tableName = DbOpenHelper.USER_TABLE_NAME; break; default:break; } return tableName; } }
- 建一个数据库
public class DbOpenHelper extends SQLiteOpenHelper { private static final String TAG = "DbOpenHelper"; private static final String DB_NAME = "BOOK"; private static final int DB_VERSION = 1; public static final String BOOK_TABLE_NAME = "book"; public static final String USER_TABLE_NAME = "user"; private String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS " +BOOK_TABLE_NAME+"(_id INTEGER PRIMARY KEY,name TEXT)"; private String CREATE_TABLE_BOOK = "CREATE TABLE IF NOT EXISTS "+ USER_TABLE_NAME+"(_id INTEGER PRIMARY KEY, name TEXT,"+ "sex INT)"; public DbOpenHelper(Context context) { super(context,DB_NAME,null,DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_BOOK); db.execSQL(CREATE_TABLE_USER); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
- 用法
Uri bookUri = Uri.parse("content://com.lzw.cha01.myapplication.chapter2.book.provider/book"); ContentValues values = new ContentValues(); values.put("_id",6); values.put("name","程序设计的艺术"); getContentResolver().insert(bookUri,values); Cursor bookCursor = getContentResolver().query(bookUri,new String[]{"_id","name"},null,null,null); while (bookCursor.moveToNext()){ Book book = new Book(); book.setBookId(bookCursor.getInt(0)); book.setBookName(bookCursor.getString(1)); Log.d(TAG, "onCreate: query book;"+book.toString()); } bookCursor.close(); Uri userUri = Uri.parse("content://com.lzw.cha01.myapplication.chapter2.book.provider/user"); Cursor userCursor = getContentResolver().query(userUri,new String[]{"_id","name","sex"},null,null,null); while (userCursor.moveToNext()){ User user = new User(); user.setName(userCursor.getString(1)); user.setSex(userCursor.getInt(2)); user.setUserId(userCursor.getInt(0)); Log.d(TAG, "onCreate: query user:"+user.toString()); } userCursor.close();
Dana.Lee
To:Dana_Lee1016@126.com