1.作业要求
请自建一个provider,然后在另一个app中使用resolver调用这个provider。
2.代码设计
本次作业要写两个程序,首先讲解CotentProvider.
MyDBHelper.java,该程序是用来创建数据库和生成数据库,当第一次建立数据库时,onCreate函数将会被调用。
public class MyDBHelper extends SQLiteOpenHelper {
public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Log.d("wyh", "MyDBHelper is running...");
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student(id integer primary key autoincrement, name varchar,age integer)");
Log.d("wyh", "DBonCreate is running...");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
接着创建MyKU.java,在这个java文件中,首先利用MyDBHelper的构造函数构建了一个名为“wyhDB”的数据库,同时通过getWriableDatabase函数使得此数据库可写。此处的uri如图中所示,是一个自定义uri,对于插入时调用的函数,withAppendId函数用来插入可变的数据,如图中的uri,而AppendId用来插入不变的,固定的信息,此时第一个参数的数据类型就变成了Builder。
public class MyKU {
private final Context context;
private SQLiteDatabase database;
public MyKU(Context context) {
this.context = context;
MyDBHelper dbHelper = new MyDBHelper(context,"wyhDB",null,1);
database =dbHelper.getWritableDatabase();
}
public Uri DAOinsert(ContentValues values){
long rowid = database.insert("student",null,values);
Uri uri = Uri.parse("content://wyh.ContentProvider/student");
Uri inserturi= ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
接着写MyContentProvider.java文件。
public class MyContentProvider extends ContentProvider {
private MyDAO myDAO;
public MyContentProvider() {
}
@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.
return myDAO.DAOinsert(values);
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
Context context = getContext();
myDAO= new MyDAO(context);
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");
}
}
以上CotentProvider程序编写完成。
然后编写CotentProvider程序, 在这里首先通过了ContentResolver类和ContentValues类创建对象,使用了put函数将需要的值填入values中,最后绑定按钮的点击事件为resolver调用insert插入函数即可。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver resolver = getContentResolver();
ContentValues values = new ContentValues();
values.put("name","wyh");
values.put("age","21");
Uri uri = Uri.parse("content://xyh.ContentProvider/student");
Button button =findViewById(R.id.button);
button.setOnClickListener(v -> {
resolver.insert(uri,values);
});
}
}
3.结果展示