ContentProvider作为四大组件之一,一直以来存在感都很低,但其实它的功能还是很强大的,尤其是在实现进程间通信的时候。和AIDL一样,ContentProvider的底层实现也是Binder,但是由于系统已经为我们做了封装,所以它的使用过程要简单的多。
内容提供者,是Android
四大组件之一。是Android中提供的专门用于不同应用间进行数据共享的方式,它天生就适合进程间通信。
通常我们的软件系统架构肯定会包含业务和数据,为了降低上层业务对底层数据的依赖,需要增加一个数据访问层来进行解耦,而Android系统为我们提供的
ContentProvider就充当了这样一个角色。
ContentProvider仅仅充当一个中间者角色,可以说是数据的“搬运工”,真正操作的数据源可以是Sqlite数据库、文件、XML和网络等。
1、创建一个自定义的ContentProvider
2、在AndroidManifest中注册这个ContentProvider
3、在ContentProvider中初始化数据源,例如创建一个数据库等
4、通过唯一标识就可以调用ContentProvider了
1、在ContentProvider的六个方法中,除了onCreate由系统回调并运行在主线程中,其它方法均由外界回调病运行在Binder线程池中;
2、android:authorities必须是唯一的;
3、ContentProvider的权限可以分为读权限和写权限,外界也必须声明对应的权限,否则外界调用时应用会异常终止;
4、query、insert、delete和update四大方法是存在多线程并发访问的,因此方法内部要做好线程同步;
5、操作数据库时一定要注意SQL语句的正确,否则程序将无法启动。特别注意空格和括号!
首先还是要声明一下,我这篇文章主要是《Android开发艺术探索》的读书总结,所以这里关于代码的部分,我是直接照着书上敲了一遍,主要是想了解使用ContentProvider的整个流程,其实真正自己敲的时候才会发现问题,然后才能更好更快的掌握,如果你是初次学习ContentProvider,一定要自己敲一遍代码!!!
1、创建一个BookProvider继承ContentProvider
public class BookProvider extends ContentProvider {
private static final String TAG = "BookProvider";
private static final String AUTHORITY = "com.example.kangbaibai.bookprovider.BookProvider";
private static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
private static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");
private static final int BOOK_URI_CODE = 0;
private static final int USER_URI_CODE = 1;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
sUriMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
}
private Context mContext;
private SQLiteDatabase mDb;
@Override
public boolean onCreate() {
Log.e(TAG, "onCreate: current thread:" + Thread.currentThread().getName());
mContext = getContext();
//ContentProvider创建时,初始化数据库。注意:实际使用中不推荐在主线程中进行耗时的数据库操作
initProviderData();
return true;
}
private void initProviderData() {
mDb = new DbOpenHelper(mContext).getWritableDatabase();
mDb.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME);
mDb.execSQL("delete from " + DbOpenHelper.USER_TABLE_NAME);
mDb.execSQL("insert into book values(3,'Android');");
mDb.execSQL("insert into book values(4,'IOS');");
mDb.execSQL("insert into book values(5,'HTML5');");
mDb.execSQL("insert into user values(1,'jake',1);");
mDb.execSQL("insert into user values(2,'jasmine',0);");
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Log.e(TAG, "query: current thread:" + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported Uri: " + uri);
}
return mDb.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
Log.e(TAG, "getType: current thread:" + Thread.currentThread().getName());
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.e(TAG, "insert: current thread:" + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported Uri: " + uri);
}
mDb.insert(table, null, values);
mContext.getContentResolver().notifyChange(uri, null);
return uri;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.e(TAG, "delete: current thread:" + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported Uri: " + uri);
}
int count = mDb.delete(table, selection, selectionArgs);
if (count > 0) {
mContext.getContentResolver().notifyChange(uri, null);
}
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.e(TAG, "update: current thread:" + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported Uri: " + uri);
}
int row = mDb.update(table, values, selection, selectionArgs);
if (row > 0) {
mContext.getContentResolver().notifyChange(uri, null);
}
return row;
}
private String getTableName(Uri uri) {
String tableName = null;
switch (sUriMatcher.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;
}
}
2、注册该ContentProvider
3、创建数据库
public class DbOpenHelper extends SQLiteOpenHelper {
static final String DB_NAME = "book_provider.db";
static final String BOOK_TABLE_NAME = "book";
static final String USER_TABLE_NAME = "user";
static final int DB_VERSION = 1;
private String CREATE_BOOK_TABLE = "CREATE TABLE IF NOT EXISTS "
+ BOOK_TABLE_NAME + "(_id integer primary key," + "name TEXT)";
private String CREATE_USER_TABLE = "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_BOOK_TABLE);
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
4、在Activity中调用
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Uri uri = Uri.parse("content://com.example.kangbaibai.bookprovider.BookProvider");
// getContentResolver().query(uri, null, null, null, null);
// getContentResolver().query(uri, null, null, null, null);
// getContentResolver().query(uri, null, null, null, null);
Uri bookUri = Uri.parse("content://com.example.kangbaibai.bookprovider.BookProvider/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);
if (bookCursor != null) {
while (bookCursor.moveToNext()) {
Book book = new Book();
book.bookId = bookCursor.getInt(0);
book.bookName = bookCursor.getString(1);
Log.e(TAG, "query book: " + book.toString());
}
bookCursor.close();
}
Uri userUri = Uri.parse("content://com.example.kangbaibai.bookprovider.BookProvider/user");
Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null);
if (userCursor != null) {
while (userCursor.moveToNext()) {
User user = new User();
user.userId = userCursor.getInt(0);
user.userName = userCursor.getString(1);
user.isMale = userCursor.getInt(2) == 1;
Log.e(TAG, "query user: " + user.toString());
}
userCursor.close();
}
}
}
到此一个简单的ContentProvider程序就搞定了,本文只是简单做个读书总结,之后我会继续深入了解ContentProvider原理,希望大家支持~
代码已上传至github:https://github.com/kb18519142009/ContentProvider
欢迎star~