前言
有了上一篇铺垫,这一篇也变得很理解的很顺畅了。
一、ContentProvoder创建步骤
那些提供外部访问接口的程序是如何实现这种功能的,如何保证其安全性,来看看如何创建的吧创建一个子类去继承ContentProvoder,有6个抽象方法,因此子类要重写这六个方法
public class MyContentProvider2 extends ContentProvider {
public MyContentProvider2() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
}
先简单讲解一些这些方法的使用
见名知意
1.onCreate() 初始化内容提供器时调用,完成对数据库的创建和升级,true表示成功,只有ContentResolver访问程序的数据时,才会初始化。
2.query() uri那一个表(数据源),可以使用UriMatcher来实现uri的 匹配,projection 表中那些列, selection和selectionArgs约束查询,sortOrder排序 ,查询结果放在Cursor中。
其他的删除增加就不赘述了。
3.getType()方法 必须实现的一个方法 获取uri得到MIME类型。
二.实现程序数据共享demo
1.内容提供的程序(简称 APP1)
为其它程序访问自己的数据提供一个接口
public class MyContentProvider extends ContentProvider {
public static final int BOOK_DIR =0;
public static final int BOOK_ITEM =1;
public static final int GATEGORY_DIR =2;
public static final int GATEGORY_ITEM =3;
public static final String AUTHORITY = " com.example.contentprovidertest.provider";
private static UriMatcher uriMatcher;
private MyDatabaseHelper dbHelper;
static { //uriMatcher进行初始化,将期望匹配的集中uri加入
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
}
public MyContentProvider() {
}
@Override
public String getType(Uri uri) {//用于uri对象获取对应的MIME
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.contentprovidertest." +
"provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.contentprovidertest." +
"provider.book";
}
return null;
}
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext(),"BookStore.db",null,2);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase(); //SQLiteDatabase 实例
Cursor cursor = null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
//将查询的数据给cursor
cursor = db.query("Book",projection,selection,selectionArgs,null,null,sortOrder);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
cursor = db.query("Book",projection,"id=?",new String[] {bookId},null,null,sortOrder);
break;
}
return cursor;
}
2.内容接收者(APP2)
APP2访问上面的程序的数据
//访问ContentProvider程序的book这个表
Button queryData = findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("lzy", "uri上面 ");
Uri uri = Uri.parse("content://com.example.contentprovidertest.provider/book");
Cursor cursor = getContentResolver().query(uri, new String[]{"price"}, null, null, null);
if (cursor!=null){
while (cursor.moveToNext()) {
@SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
@SuppressLint("Range") String author = cursor.getString(cursor.getColumnIndex("anthor"));
@SuppressLint("Range") int pager = cursor.getInt(cursor.getColumnIndex("pages"));
@SuppressLint("Range") double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("lzy", "book name is" + name);
Log.d("lzy", "book author is" + author);
Log.d("lzy", "book pages is" + pager);
Log.d("lzy", "book price is" + price);
}
cursor.close();
}
}
});
}
在APP1中创建了一张book表数据
在APP2中访问它的数据 程序运行结果如图
总结
好了内容提供者到处介绍完毕,如何访问另外一个程序就完成了。
到时候出一个篇三,将着重讲,关于Content Provider我踩过的哪一些坑,吸取经验。