将雪梨任务01 生词本作业中生成的生词本数据库通过自定义ContentProvider的方式,共享给其他应用。
要求如下:
(1) 使用自定义SQLiteOpenHelper来管理数据库;
(2) 提交作业应列出操作数据的Uri及数据表的字段名称;
(3) 提交作业应给出自定义的CP文件的核心代码。
点击下载自定义ContentProvider实现生词本数据库共享的相关代码
WordDatabaseHelper(自定义SQLiteOpenHelper):
package com.example.homework02contentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class WordDatabaseHelper extends SQLiteOpenHelper {
public WordDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* 数据库不存在,第一次创建数据库时被自动调用,并且只被调用一次
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table WordTB(" + MyWord.MyStarTable._ID +" integer primary key autoincrement,"+ MyWord.MyStarTable.WORD +" varchar(20) unique,"+ MyWord.MyStarTable.MEAN +" varchar(200))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MyWord:
package com.example.homework02contentprovider;
import android.net.Uri;
import android.provider.BaseColumns;
/**
* 用预定义数据表的结构的Java Bean
*/
public class MyWord {
//定义Authority
public static final String AUTHORITY = "net.onest.lrf.provider";
//定义内部表结构(列名)
public static final class MyStarTable implements BaseColumns {
//定义数据表的列名称
public final static String _ID = "id";
public final static String WORD = "word";
public final static String MEAN = "mean";
//定义两个Uri
public final static Uri WORDS_CONTENT_URI =
Uri.parse("content://" + AUTHORITY + "/words");
public final static Uri WORD_CONTENT_URI =
Uri.parse("content://"+AUTHORITY+"/word/#");//#可以代表任意的数字
}
}
操作数据的Uri:
content://net.onest.lrf.provider/words
content://net.onest.lrf.provider/word/#
数据表的字段名称:
id integer primary key autoincrement,
word varchar(20) unique,
mean varchar(200)
WordContentProvider(自定义的CP文件的核心代码):
package com.example.homework02contentprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class WordContentProvider extends ContentProvider {
//定义两个标号
private static final int WORDS = 1;
private static final int WORD = 2;
//定义数据库管理类的对象属性
private WordDatabaseHelper dbHelper;
//定义UriMatcher属性
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
//注册两个Uri并关联Uri与标号
static {
matcher.addURI(MyWord.AUTHORITY,"/words",WORDS);
matcher.addURI(MyWord.AUTHORITY,"/word/#",WORD);
}
public WordContentProvider() {
}
/**
* 删除数据
* @param uri
* @param selection
* @param selectionArgs
* @return
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
//获取可写的数据库对象
SQLiteDatabase db = dbHelper.getWritableDatabase();
int n = 0;
switch (matcher.match(uri)){
case WORDS:
n = db.delete("WordTB",selection,selectionArgs);
break;
case WORD:
long id = ContentUris.parseId(uri);
//重新拼接where条件
String where = "id=" + id;
if(null != selection && !selection.equals("")){
where = "id = " + id + " and " + selection;
}
//执行数据库删除的操作
n = db.delete("WordTB",where,selectionArgs);
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
return n;
}
@Override
public String getType(Uri uri) {
String type = null;
//根据参数的形式(批量/单一 操作)来返回相应的MIME类型
switch (matcher.match(uri)){
case WORDS://表示批量操作数据
type = "lrf.cursor.dir/net.onest.lrf.provider";
break;
case WORD://表示单一数据操作
type = "lrf.cursor.item/net.onest.lrf.provider";
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
return type;
}
/**
* 插入数据
* @param uri
* @param values
* @return
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
//实现数据的插入操作
switch (matcher.match(uri)){
case WORD:
//获取可写的数据库对象
SQLiteDatabase db = dbHelper.getWritableDatabase();
//执行插入操作
long rowId = db.insert("WordTB",null,values);
if(rowId > 0){
//插入成功
//拼接Uri和被插入的数据的id
return ContentUris.withAppendedId(uri,rowId);
}else{
//插入失败
return null;
}
default:
throw new UnsupportedOperationException("Not yet implemented");
}
}
@Override
public boolean onCreate() {
dbHelper = new WordDatabaseHelper(getContext(),"WordsDB.db",null,1);
return true;
}
/**
* 查询数据
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @return
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//获取可读的数据库对象
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
switch (matcher.match(uri)){
case WORDS://批量查询操作
cursor = db.query("WordTB",projection,selection,selectionArgs,null,null,sortOrder);
break;
case WORD://单一查询操作
//解析Uri中的id
long id = ContentUris.parseId(uri);
//重新拼接where条件
String where = "id=" + id + " and " + selection;
//获取数据库执行查询操作
cursor = db.query("WordTB",projection,where,selectionArgs,null,null,sortOrder);
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
return cursor;
}
/**
* 数据更新/数据修改
* @param uri
* @param values
* @param selection
* @param selectionArgs
* @return
*/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
//获取可写的数据库对象
SQLiteDatabase db = dbHelper.getWritableDatabase();
int n = 0;
switch (matcher.match(uri)){
case WORDS:
n = db.update("WordTB",values,selection,selectionArgs);
break;
case WORD:
//解析Uri中的id
long id = ContentUris.parseId(uri);
//重新拼接where条件
String where = "id=" + id;
if(selection != null && !selection.equals("")){
where = "id=" + id + " and " + selection;
}
//执行数据库更新操作
n = db.update("WordTB",values,where,selectionArgs);
break;
default:
throw new UnsupportedOperationException("Not yet implemented");
}
return n;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.homework02contentprovider">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name=".WordContentProvider"
android:authorities="net.onest.lrf.provider"
android:enabled="true"
android:exported="true"></provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>