contentResolver调用自己的一些方法,然后contentprovider就会去执行相应的方法,然后将返回结果返回,我们得到我们想要的数据。
contentprovider是安卓四大组件之一,使用其方法类进行数据获取;
自建一个provider,然后在另一个app中使用resolver调用这个provider。
一个应用的数据文件是应用私有的,其他应用不能直接访问。当一个应用需要访问另一个应用的数据库表数据时,当前应用使用ContentProvider将数据库表操作暴露给其他应用访问,其他应用需要使用ContentResolver来调用ContentProvider的方法。用两个APP进行实验(ContentProvider和MyResolver)
- MyResolver
用ContentResolver对象来对其它程序中内容提供器进行访问;且需要一个uri,用来明确我们要访问这个程序中的哪个表或者哪个数据。
getContentResolver()方法获取到这个对象。
ContentResolver resolver=getContentResolver();
uri:用来指明我们需要访问的哪个程序中的哪个表,由三个部分组成,前缀、Authority、path 。用Uri的parse()方法来进行解析。
Uri uri=Uri.parse("content://rain.provider/student");
contentValues用来存放数据
ContentValues values=new ContentValues();
values.put("name","rain");
values.put("age",20);
在清单文件中声明一个<query>标签用于声明我们要访问的程序的provider。或者指定一个我们要访问的包名。
<queries>
<package android:name="com.example.contentprovider"></package>
</queries>
MainActivity.java
package com.example.myresolver;
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","rain");
values.put("age",20);
Uri uri=Uri.parse("content://rain.provider/student");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {resolver.insert(uri,values);}
});
}
}
2. ContentProvider
contentProvider--内容提供器,提供给其它app访问本app中的数据,相当于一个接口。
首先要实现一个 ContentProvider类,完成对数据的CRUD操作;ContentProvider类中需要对SQLite数据库进行操作,要创建一个DBHelper类来得到一个数据库,这个类继承自SQLiteOpenHelper ,访问 ContentProvider的数据用ContentResolver。
DbHelper.java 生成一个 SQLite数据库
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 {
public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Log.d("rain","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("rain","onCreate is running...");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
ContentProvider.java 对数据库进行访问操作
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");
}
}
MyDA.java
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,"rainDB",null,1);
database=dBhelper.getWritableDatabase();
}
public Uri DAOinsert(ContentValues contentValues){
long rowid=database.insert("student",null,contentValues);
Uri uri=Uri.parse("content://rain.provider");
Uri inserturi=ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
运行结果: