文章目录
- 一,设计目标
- 二,功能说明
- 三,代码解析
- 四,运行展示截图
- 五,源码仓库地址
一、设计目标
1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。
3、本次作业请启用新项目,理论上需要两个APP进行实验。
二、功能说明
在Resolver中点击insert,向Provider中插入数据,在第二个APP:Resolver中调用第一个APP:Provider。
三、 代码解析
Provider
MainActivity.java
在其中只需要创建一个MyDAO的对象
package com.example.provider;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDAO myDAO=new MyDAO(this);
}
}
MyDAO.java
在这个java文件中,首先利用MyDBHelper的构造函数构建了一个名为“cqDB”的数据库,版本为1
package com.example.provider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyDAO {
private Context context;
private SQLiteDatabase database;
public MyDAO(Context context) {
this.context=context;
MyDBhelper dBhelper=new MyDBhelper(context,"cqDB",null,1);
database=dBhelper.getWritableDatabase();
}
public Uri DAOinsert(ContentValues contentValues){
long rowid=database.insert("student",null,contentValues);
Uri uri=Uri.parse("content://cq.provider2");
Uri inserturi=ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
MyDBhelper.java
这个文件是用来创建数据库和生成数据库,当第一次建立数据库时会进行打印。
package com.example.provider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
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("cq","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("cq","onCreate is running.");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MyContentProvider.java
调用MyBAO的构造器
package com.example.provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
public class MyContentProvider extends ContentProvider {
private MyDAO myDb;
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 myDb.DAOinsert(values);
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
Context context=getContext();
myDb=new MyDAO(context);
return true;
}
@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");
}
}
Resolver
MainActivity.java
使用了put函数将需要的值填入values中,使用绑定按钮的点击事件为resolver调用insert插入函数
vpackage com.example.resolver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver resolver=getContentResolver();
Button button=findViewById(R.id.button);
ContentValues values=new ContentValues();
values.put("name","cq");
values.put("age",20);
Uri uri=Uri.parse("content://cq.provider2/student");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resolver.insert(uri,values);
}
});
}
}
activity_main.xml
进行界面设计添加一个TextView和一个Button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="resolver"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.315" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
四、运行展示截图
Resolver运行界面
插入了数据
五、源码仓库地址
Provider
https://gitee.com/Chen__address/Provider
Resolver