设计目标:
1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。
3、本次作业请启用新项目,理论上需要两个APP进行实验。
功能说明:
1、contentprovider主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性。
2、在本次实验中具体表现为以下效果,在第一个APP: myprovide中可以切换访问第二个APP: myresolver,myresolver可以调用myprovider。
代码解析:
myresolver:
package com.example.myresolver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentProvider;
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 {
private Button button;
// private static final String AUTHORITY = "wxy.provider";
// private static final Uri NOTIFY_URI=Uri.parse("content://" + AUTHORITY + "/person");
// Uri uri = Uri.parse("content://wxy.provider1/person");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button1);
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://wxy.provider1/student");
ContentValues values = new ContentValues();
values.put("name","wxy");
values.put("age",20);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resolver.insert(uri,values);
}
});
}
}
myprovider:
MyContentProvider.java:
package com.example.myprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
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.
//throw new UnsupportedOperationException("Not yet implemented");
Context context=getContext();
myDao=new MyDao(context);
Log.d("wxy","MyDBHelper is running");
return myDao.wxyInsert(values);
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
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");
}
}
MyDao.java:
package com.example.myprovider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
public class MyDao {
Context context;
public MyDao(Context context){
this.context=context;
}
MyDataBasehelper myDBhelper = new MyDataBasehelper(context,"wxyDB",null,1);
SQLiteDatabase database = myDBhelper.getWritableDatabase();
public Uri wxyInsert(ContentValues values){
// ContentValues values1 = new ContentValues();
// values1.put("name","zhangsan");
// values1.put("age",22);
Log.d("wxy","MywxyInsert is running");
Uri uri=Uri.parse("content://wxy.provider1/student");
long rowId=database.insert("student",null,values);
Uri inserturi= ContentUris.withAppendedId(uri,rowId);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
MyDataBasehelper:
package com.example.myprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyDataBasehelper extends SQLiteOpenHelper {
public MyDataBasehelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
//name 数据库名字,factory 游标 null version 数据库版本
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student " +
"(id integer primary key autoincrement,name varchar(20),age integer)");
//执行标准SQL语句
Log.d("wxy","MyDBHelper is running");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
运行展示截图: