作业要求
1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。
3、本次作业请启用新项目,理论上需要两个APP进行实验。
实验过程
自建一个provider:
1.Mydbhelper.java类用于创建数据库表
package com.example.contentprovider;
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 { //名字 nullable允许为null 游标工厂
public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Log.d("wyw","MyDBhelper is run....");
}
//未定义数据库访问规则 所以onCreate无法启用
@Override //建表
public void onCreate(SQLiteDatabase sqLiteDatabse) {
//建table表
sqLiteDatabse.execSQL("create table student(id integer primary key autoincrement,name varchar,age integer)");
Log.d("wyw","oncreate is run....");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.MYDAO编写工具类用于数据库增删改
package com.example.contentprovider;
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,"wywDB",null,1);
database = dBhelper.getWritableDatabase();
}
public Uri DAOinsert(ContentValues contentValues){
long rowid = database.insert("student",null,contentValues);
Uri uri=Uri.parse("content://wyw.provider2");
Uri inserturi = ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
3.MyContentProvider编写自己的ContenProvider
package com.example.contentprovider;
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 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.
//getContext().getContentResolver().insert(uri,values);
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");
}
}
在另一个app中使用resolver调用这个provider
使用ContentResolver调用ContentProvider提供的接口,对ContentProvider中的数据进行添加、删除、修改和查询操作时。
package com.example.resovler;
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();
ContentValues values = new ContentValues();
values.put("name","wyw");
values.put("age",21);
Uri uri=Uri.parse("content://wyw.provider2/student");
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resolver.insert(uri,values);
}
});
}
}
实验结果:
实验小结:
ContendProvider是不同应用程序之间进行数据交换的标准API,ContentProvider以某种Uri的形式对外提供数据,允许其他应用访问或修改数据;其他应用程序使用ContentReslover根据Uri去访问操作指定数据,是Android四大组件之一,主要的作用是:实现各个应用程序之间的(跨应用)数据共享。ContentProvider提供了在应用程序之前共享数据的一种机制。