contentProvider是Android中提供的专门用于不同应用间进行数据共享方式,作为四大组件之一的contentProvider,使用起来比较简单,但是因为使用频率不是很高,所以经常遗忘其使用方法,所以在次记录下其基本用法的实例。
首先,有必要对contentProvider的基本原理进行一个简单的讨论。
contentProvider的底层实现是通过Binder实现,但是他的使用比aidl要简单很多,因为系统已经为我们做了很多的封装。
系统中也为我们提供了非常多的ContentProvider,比如通讯录,日程表,通话记录等,要跨进程访问这些信息,只需要通过ContentResolver的query、update、insert、update、insert、delete方法即可。
要实现自定义ContentProvider,继承ContentProvider类并实现六个抽象方法即可,这六个抽象方法分别是是:OnCreate、query、update、insert、delete和getType。需要注意的是在这六个方法中,除了OnCreate由系统回调并运行在主线程里,其他五个方法均由外界回调并运行在Binder线程池中。
下面我们自定义一个BookProvider继承自ContentProvider,其代码如下:
public class BookProvider extends ContentProvider {
public static final String TAG = "reoger.hut.hello.word";
public static final String AUTHORLTY = "reoger.hut.hello.word";
public static final Uri BOOK_CONTENT_URI = Uri.parse("content://"+AUTHORLTY+"/book");
public static final Uri USER_CONTENT_URI = Uri.parse("content://"+AUTHORLTY+"/user");
public static final int BOOK_URI_CODE = 0;
public static final int USER_URI_CODE = 1;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private Context mContext;
private SQLiteDatabase mDb;
static {
sUriMatcher.addURI(AUTHORLTY,"book",BOOK_URI_CODE);
sUriMatcher.addURI(AUTHORLTY,"user",USER_URI_CODE);
}
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;
}
@Override
public boolean onCreate() {
Log.d(TAG, "onCreate: " + Thread.currentThread());
mContext = getContext();
initProvider();
return true;
}
/**
* 初始化数据库
*/
private void initProvider() {
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,'jack',1);");
mDb.execSQL("insert into user values(2,'jany',0);");
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Log.d(TAG, "query: " + Thread.currentThread().getName());
String table = getTableName(uri);
if(table == null){
throw new IllegalArgumentException(" UnSupported URL: "+uri);
}
return mDb.query(table,projection,selection,selectionArgs,null,null,sortOrder,null);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.d(TAG, "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(TAG, "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(TAG, "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;
}
}
因为其共享的内容通过数据库存储,所以需要一个数据库的帮助类。用来帮我们创建数据库,代码很简单,DbOpenHelper的代码如下:
public class DbOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "boook_provider.db";
public static final String BOOK_TABLE_NAME = "book";
public static final String USER_TABLE_NAME = "user";
private 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) {
}
}
需要说明的是,CongetProvider可以共享的数据不仅只是数据库的,也可以共享文件中的数据,甚至可以共享内存中的数据。这里只是通过共享数据库中的内容作为示例 。
当然,作为android四大组件,都是需要在manifest中进行申明(当然,broadcast也可以在java代码中进行声明)。所以这里BookProvider声明的代码如下:
<provider
android:name=".BookProvider"
android:authorities="reoger.hut.hello.word"
android:permission="com.reoger.hello"
android:process=":provider"
/>
其中authorities属性就声明了外界访问这个ContentProvider的Uri。例如此处访问的URI的值应该是:content://reoger.hut.hello.word 。
通过permission属性为provider定制权限,需要访问此provider必须声明此处定义的permission,否则无法正常获取到信息。当然,为了让其运行在一个单独的进程中,指定了process属性。
通过前面的代码,自定义的ContentProvider已经实现完毕,接下来我们来检验自定义的BookProvider是否正常工作。
MainActivity代码如下;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uri = Uri.parse("content://reoger.hut.hello.word/book");
ContentValues values = new ContentValues();
values.put("_id",6);
values.put("name","Android 开发艺术");
getContentResolver().insert(uri,values);
Cursor bookCursor = getContentResolver().query(uri,new String[]{"_id","name"},null,null,null);
while(bookCursor.moveToNext()){
Book book = new Book(bookCursor.getInt(0),bookCursor.getString(1));
Log.d("TAG","bookID "+book.toString());
}
bookCursor.close();
Uri uri2 = Uri.parse("content://reoger.hut.hello.word/user");
ContentValues values2 = new ContentValues();
values2.put("_id",3);
values2.put("name","reoger");
values.put("sex","0");
getContentResolver().insert(uri2,values2);
Cursor bookCursor2 = getContentResolver().query(uri2,new String[]{"_id","name","sex"},null,null,null);
while(bookCursor2.moveToNext()){
User user = new User(bookCursor2.getInt(0),bookCursor2.getString(1),bookCursor2.getInt(2));
Log.d("TAG","bookID "+user.toString());
}
bookCursor2.close();
}
}
通过打印的日志信息,发现我们的测试示例完全符合我们的期望。结果如图所示:
最后,补充一下其中Book和User的代码:
public class Book implements Parcelable{
public Book(int id, String name) {
this.id = id;
this.name = name;
}
private int id;
private String name;
public Book() {
}
protected Book(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
}
@Override
public String toString() {
String result = "book = ["+id+
","+name+"];";
return result;
}
}
public class User implements Parcelable {
private int id;
private String name;
private int sex;
public User(int id, String name, int sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "id= "+id+" ,name ="+name+" ,sex= "+sex;
}
protected User(Parcel in) {
id = in.readInt();
name = in.readString();
sex = in.readInt();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeInt(sex);
}
}