1ContentProvider是Android中提供的专门用于不同应用间进行数据共享的方式,从这一点看,它天生适合进程间通信。和Messenger一样,ContentProvider的底层实现也是Binder。系统预置了许多ContentProvider,比如通讯录信息,日程表信息等,要跨进程的访问这些信息,只需要通过ContentReslover的query,update,insert,和delete方法.
2 使用方法
首先,我们创建一个ContentProvider,实现六个抽象方法即可:onCreate,query,update,insert,delete和getType.onCreate代表ContentProvider的创建,一般用来做一些初始化的操作;getType用来返回一个Uri请求对应的MIME类型(媒体类型),比如图片,视频等。根据Binder的工作原理,我们知道这六个方法均运行在ContentProvider的进程中,除了onCreate()有系统回调并运行在主线程里,其他五个方法均有外界回调并运行在Binder线程池中。
3直接看代码
package com.zhouzhuo.conentprovidermaster;
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;
/**
* Created by zhouzhuo on 2018/1/3.
*/
public class BookProvider extends ContentProvider {
private static final String TAG = "BookProvider";
private static final String AUTHORITY = "com.zhouzhuo.conentprovidermaster.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");
public static final int BOOK_URI_CODE = 0;
public static final int USER_URI_CODE = 1;
private static final UriMatcher sUriMather = new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMather.addURI(AUTHORITY,"book",BOOK_URI_CODE);
sUriMather.addURI(AUTHORITY,"user",USER_URI_CODE);
}
private Context mContext;
private SQLiteDatabase mDb;
@Override
public boolean onCreate() {
Log.d("zhouzhuo","onCreate,current thread:"+Thread.currentThread().getName());
mContext = getContext();
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.d("zhouzhuo","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);
}
private String getTableName(Uri uri) {
String tableName = null;
switch (sUriMather.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;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
Log.d("zhouzhuo","getType");
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.d("zhouzhuo","insert");
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.d("zhouzhuo","delete");
String table = getTableName(uri);
if(table == null){
throw new IllegalArgumentException("Unsupported URI:"+uri);
}
int count = mDb.delete(table,selection,selectionArgs);
if(count>0){
getContext().getContentResolver().notifyChange(uri,null);
}
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.d("zhouzhuo","update");
String table = getTableName(uri);
if(table == null){
throw new IllegalArgumentException("Unsupported URI:"+uri);
}
int row = mDb.update(table,values,selection,selectionArgs);
if(row>0){
getContext().getContentResolver().notifyChange(uri,null);
}
return row;
}
}
package com.zhouzhuo.conentprovidermaster;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by zhouzhuo on 2018/1/4.
* 数据库管理,创建表
*/
public class DbOpenHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "book_provider.db";
public static final String BOOK_TABLE_NAME = "book";
public static final String USER_TABLE_NAME = "user";
private static final int DB_VERSION = 3;
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) {
}
}
package com.zhouzhuo.conentprovidermaster;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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);
initData();
}
private void initData() {
Uri bookUri = Uri.parse("content://com.zhouzhuo.conentprovidermaster.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);
while (bookCursor.moveToNext()){
Book book = new Book();
book.bookId = bookCursor.getInt(0);
book.bookName = bookCursor.getString(1);
Log.d("zhouzhuo","query book:"+book.toString());
}
bookCursor.close();
Uri userUri = Uri.parse("content://com.zhouzhuo.conentprovidermaster.BookProvider/user");
Cursor userCursor = getContentResolver().query(userUri,new String[]{"_id","name","sex"},null,null,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.d("zhouzhuo","query user:"+user.toString());
}
userCursor.close();
}
}