1.实现跨程序数据共享
在拥有SQLite数据库的程序上,通过提供自定义ContentProvider,从而向其他程序提供访问原程序数据的接口。
自定义ContenProvider一定要在AndroidManifest.xml文件中注册才可以使用。
在调用程序中,通过正确的内容URI和getContentResolver()方法获取ContentResolver后,就可以调用原程序中的数据。
public class MyContentProvider extends ContentProvider {
//与UriMatcher匹配的自定义代码
public static final int BOOK_DIR = 0;
public static final int BOOK_ITEM = 1;
public static final int CATEGORY_DIR = 2;
public static final int CATEGORY_ITEM = 3;
public static final String AUTHORITY = "com.futuring.app.sharedata";
private static UriMatcher sMatcher;
//数据库
private MyDatabaseHelper dbHelper;
//利用UriMatcher将内容URI与自定义代码匹配
static {
sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI(AUTHORITY, "book", BOOK_DIR);
sMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);
sMatcher.addURI(AUTHORITY, "category", CATEGORY_DIR);
sMatcher.addURI(AUTHORITY, "category/#", CATEGORY_ITEM);
}
public MyContentProvider() {
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
dbHelper = new MyDatabaseHelper(getContext(), "BookStore.db", null, 2);
return true;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
SQLiteDatabase db = dbHelper.getWritableDatabase();
int deletedRows = 0;
switch (sMatcher.match(uri)) {
case BOOK_DIR:
deletedRows = db.delete("Book", selection, selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
deletedRows = db.delete("Book", "id = ?", new String[]{bookId});
break;
case CATEGORY_DIR:
deletedRows = db.delete("Category", selection, selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
deletedRows = db.delete("Category", "id = ?", new String[]{categoryId});
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
//返回被删除的行数
return deletedRows;
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
switch (sMatcher.match(uri)) {
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.futuring.app.sharedata.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.futuring.app.sharedata.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.futuring.app.sharedata.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.futuring.app.sharedata.category";
}
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.
SQLiteDatabase db = dbHelper.getWritableDatabase();
Uri uriReturn = null;
switch (sMatcher.match(uri)) {
case BOOK_DIR:
case BOOK_ITEM:
//db.insert返回新插入数据的id
long newBookId = db.insert("Book", null, values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + newBookId);
break;
case CATEGORY_DIR:
case CATEGORY_ITEM:
//db.insert返回新插入数据的id
long newCategoryId = db.insert("Category", null, values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + newCategoryId);
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
//返回新插入数据的Uri
return uriReturn;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
switch (sMatcher.match(uri)) {
case BOOK_DIR:
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;
case CATEGORY_DIR:
cursor = db.query("Category", projection, selection, selectionArgs,
null, null, sortOrder);
break;
case CATEGORY_ITEM:
//获取以id为结尾的内容URI "..../#" #代表的变量
String categoryId = uri.getPathSegments().get(1);
cursor = db.query("Categroy", projection, "id = ?",
new String[]{categoryId}, null, null, sortOrder);
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
SQLiteDatabase db = dbHelper.getWritableDatabase();
int updateRows = 0;
switch (sMatcher.match(uri)) {
case BOOK_DIR:
updateRows = db.update("Book", values, selection, selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
updateRows = db.update("Book", values, "id = ?",
new String[]{bookId});
break;
case CATEGORY_DIR:
updateRows = db.update("Category", values, selection, selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
updateRows = db.update("Category", values, "id = ?",
new String[]{categoryId});
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
//返回被更新数据的行数
return updateRows;
}
}