ContentProvider#
ContentProvider 可以帮助应用程序管理对自己存储的数据的访问, 由其他应用存储,并提供与其他应用共享数据的方法。它们封装了数据,并提供定义数据安全性的机制。内容提供商是标准 将一个进程中的数据与另一个进程中运行的代码连接起来的接口
ContentProvider 方法
- onCreate:创建 ContentProvider 后调用
- getType:返回当前 Uri 所代表数据的 MIME 类型
- query:用于供外部应用从 ContentProvider 中查询数据
- insert:用于供外部应用向 ContentProvider 中增加数据
- delete:用于供外部应用从 ContentProvider 中删除数据
- update:用于供外部应用更新 ContentProvider 中数据
首先 manifest 声明,有两种方式(Android 11 更新后改变了当前应用于本机其他应用进行交互的方式)保险都写一遍
<application>
...
<provider
android:name=".MyContentProvider"
android:authorities="com.example.provider.MyContentProvider"
android:enabled="true"
android:exported="true">
</provider>
</application>
<queries>
<provider android:authorities="com.example.provider.MyContentProvider"
android:enabled="true"
android:exported="true" />
</queries>
官方文档说明
https://developer.android.google.cn/guide/topics/providers/content-provider-creating#ContentURI
Uri#
内容 URI 是标识提供程序中的数据的 URI。内容 URI 包括 整个提供程序的符号名称(其权限)和 指向表或文件的名称(路径)。可选 id 部分指向 表中的单个行。每个数据访问方法都有一个内容 URI 作为参数;这使您可以 确定要访问的表、行或文件
官方建议
https://developer.android.google.cn/guide/topics/providers/content-provider-creating?hl=en#kotlin
eq.
content://com.example.app.provider/table1
:调用的表。table1com.example.app
包名provider
ContentProvider java 文件名
和数据库增删查改类似,定义好数据库的增删查改,用 ContentProvider 包装一下
如下
- mydb 中封装 sql 操作
- ContentProvider 中调用,返回游标
- 最后使用
Provider 对应 app#
定义数据库,创建学生表,3 个字段#
mydb.java
public class mydb extends SQLiteOpenHelper {
public mydb(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student ("+
"id integer primary key autoincrement,name varchar,age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
数据库操作类(只写了插入,查询)#
mydao.java
public class mydao {
private Context context;
private SQLiteDatabase database;
public mydao(Context context){
this.context=context;
mydb dbhelper=new mydb(context,"db",null,1);
database=dbhelper.getWritableDatabase();
Log.d("qwe","getWritableDatabase");
}
public Uri Daoinsert(ContentValues contentValues){
long rowid=database.insert("student",null,contentValues);
Uri uri=Uri.parse("content://com.example.provider.MyContentProvider/student");
Uri inserturi=ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
public Cursor query(String[] whichone,String selection,String[] selectionArgs,String sortOrder) {
Log.d("qwe","ok");
Cursor cursor1 = database.query("student",whichone,selection,selectionArgs,
null,null,sortOrder);
return cursor1;
}
}
MYContentProvider.java
(只用了查,增操作)
public class MyContentProvider extends ContentProvider {
private mydao mydao;
public MyContentProvider() {
}
@Override
public String getType(Uri uri) {
return "1";
}
@Override
public Uri insert(Uri uri, ContentValues values) {
getContext().getContentResolver().insert(uri,values);
return mydao.Daoinsert(values);
}
@Override
public boolean onCreate() {
mydao=new mydao(this.getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return mydao.query(projection,selection,selectionArgs,sortOrder);
}
}
resovler app#
以下下查询content://com.example.provider.MyContentProvider/student
的 age>0 的内容
注释的地方是 insert 操作,
public class MainActivity extends AppCompatActivity {
private String d="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button_resovel);
TextView textView=findViewById(R.id.textView_resovel);
ContentResolver resolver = getContentResolver();
ContentValues values= new ContentValues();
values.put("name","sb");
values.put("age","20");
Uri uri = Uri.parse("content://com.example.provider.MyContentProvider/student");
// button.setOnClickListener(v -> resolver.insert(uri,values));
button.setOnClickListener(v -> {
Cursor cursor = resolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
@SuppressLint("Range") String name = cursor.getString(cursor.
getColumnIndex("name"));
@SuppressLint("Range") int age = cursor.getInt(cursor.
getColumnIndex("age"));
@SuppressLint("Range") int id = cursor.getInt(cursor.
getColumnIndex("id"));
Log.d("db", "id=" + id + "|name=" + name + "|age=" + age);
d += ("id=" + id + "|name=" + name + "|age=" + age);
}
textView.setText(d);
}
);
}
}
结果截图#
左边提供者,右边接收者