本次实验我使用了两个app,Mycontentprovider和Myresolver
Contentprovider是安卓四大组件之一,ContentProvider方法有
·onCreate:创建ContentProvider后调用
getType:返回当前Uri所代表数据的MIME类型
query:用于供外部应用从ContentProvider中查询数据
insert:用于供外部应用向ContentProvider中增加数据
delete:用于供外部应用从ContentProvider中删除数据
update:用于供外部应用更新ContentProvider中数据
MyContentprovider部分:
main activity
package com.example.mycontentprovider;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private Object MyDAO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDAO myDAO=new MyDAO(this);
}
}
数据库操作类MyDAO
package com.example.mycontentprovider;
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,"zzyDB",null,1);
database = dBhelper.getWritableDatabase();
}
public Uri DAOinsert(ContentValues contentValues){
long rowid= database.insert("student",null,contentValues);
Uri uri=Uri.parse("content://zzy.provider2");
Uri inserturi= ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
MyContentProvider
package com.example.mycontentprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
class MyContentProvider extends ContentProvider {
private MyDAO myDAO;
public MyContentProvider() {
// MyDAO myDAO= new MyDAO(this.getContext());
}
@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) {
// getContext().getContentResolver().insert(uri, values);
return myDAO.DAOinsert(values);
}
@Override
public boolean onCreate() {
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");
}
}
MyDBhelper
package com.example.mycontentprovider;
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);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// 执行sql语句
sqLiteDatabase.execSQL("create table student(" +
"id integer primary key autoincrement,name varchar,age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
Myresolver部分
main activity
package com.example.myresovler;
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","zzy");
values.put("age","21");
Uri uri=Uri.parse("content://zzy.provider2/student");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {resolver.insert(uri,values);}
});
}
}
点击按钮后插入数据成功