内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性。目前,使用内容提供器是android实现跨程序共享数据的标准方式。
内容提供者可以把私有的数据库暴露出来。
内容提供者把数据进行封装然后提供出来,其他应用都是通过内容解析者来访问。
实现内容提供者的步骤:
1.定义一个类继承ContentProvider
2.在清单文件里配置内容提供器
3.写一个静态代码块添加匹配规则
4.暴露自己想暴露的方法(增删改查)
5.其他应用就可以通过内容提供者去操作数据库
DBHelper.class
package com.example.mycontentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "atguigu.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//建表
String sql = "create table person(_id integer primary key autoincrement,name varchar)";
sqLiteDatabase.execSQL(sql);
sqLiteDatabase.execSQL("insert into person (name) values ('Tom1')");
sqLiteDatabase.execSQL("insert into person (name) values ('Tom2')");
sqLiteDatabase.execSQL("insert into person (name) values ('Tom3')");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MyContentProvider.class
package com.example.mycontentprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyContentProvider extends ContentProvider {
/*********************************************************************************************************/
//UriMatcher其实就是用来存放所有合法的uri的一个容器
private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//保存一些合法的uri
// content://com.example.mycontentprovider/person 不根据id查询
// content://com.example.mycontentprovider/person/2 根据id查询
static {
uriMatcher.addURI("com.example.mycontentprovider","/person",1);
uriMatcher.addURI("com.example.mycontentprovider","/person/#",2); //#就是匹配任意数字 这里写个2仅仅是占位而已
}
/*********************************************************************************************************/
/*********************************************************************************************************/
//数据库操作类:DBHelper
private DBHelper dbHelper;
/********************************************************************************************************/
public MyContentProvider() {
}
@Override
public boolean onCreate() {
dbHelper = new DBHelper(getContext());
return false;
}
/***********************************删除 删除 删除***************************************************/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
//得到连接对象
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
//匹配uri,返回code
int code = uriMatcher.match(uri);
//如果合法,进行删除
if (code == 1){ //删除的个数
int deleteCount = readableDatabase.delete("person", selection, selectionArgs);
return deleteCount;
}else if (code == 2){
//得到uri尾巴带的参数
long id = ContentUris.parseId(uri);
int deleteCount = readableDatabase.delete("person", "_id="+id, null);
return deleteCount;
}else {
readableDatabase.close();
throw new RuntimeException("插入的uri不合法");
}
}
/***********************************插入 插入 插入***************************************************/
// content://com.example.mycontentprovider/person 不根据id插入
// content://com.example.mycontentprovider/person/2 根据id插入(没有)
@Override
public Uri insert(Uri uri, ContentValues values) {
//得到连接对象
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
//匹配uri,返回code
int code = uriMatcher.match(uri);
//如果合法,进行插入
if (code == 1){
//插入会返回一个新的id
long id = readableDatabase.insert("person", null, values);
//将id添加到uri中 变成一个新的uri
// 随便写,供日志方便查看
uri = ContentUris.withAppendedId(uri,id);
readableDatabase.close();
return uri;
}else {
readableDatabase.close();
throw new RuntimeException("插入的uri不合法");
}
//如果不合法,抛出异常
}
/***********************************查询 查询 查询***************************************************/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//得到连接对象
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
//1.匹配URI,返回code
int code = uriMatcher.match(uri);
//如果合法,进行查询
if (code == 1){ //不根据id查询
Cursor cursor = readableDatabase.query("person", projection, selection, null, null, null, null);
return cursor;
}else if (code == 2){ //根据id查询
//得到id=2
long id = ContentUris.parseId(uri);
//查询
Cursor cursor1 = readableDatabase.query("person", projection, "_id=?", new String[]{id + ""}, null, null, null);
return cursor1;
}else { //如果不合法,抛出异常
throw new RuntimeException("查询的uri不合法");
}
}
/***********************************更新 更新 更新***************************************************/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
//得到连接对象
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
//匹配uri,返回code
int code = uriMatcher.match(uri);
//如果合法,进行更新
if (code == 1){ //删除的个数
int update = readableDatabase.update("person", values, selection, selectionArgs);
return update;
}else if (code == 2){
//得到uri尾巴带的参数
long id = ContentUris.parseId(uri);
int update = readableDatabase.update("person", values,"_id="+id, null);
return update;
}else {
readableDatabase.close();
throw new RuntimeException("插入的uri不合法");
}
}
@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");
}
}
MainActivity.class
package com.example.mycontentprovider;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
/********************************uri最后面带着的那个尾巴就是数据库表的 id id id*******/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void queryById(View view){
//1.得到ContentResolver对象
ContentResolver resolver = getContentResolver();
//2.调用其query 得到cursor
Uri uri = Uri.parse("content://com.example.mycontentprovider/person/1");
Cursor cursor = resolver.query(uri, null, null, null, null);
//3.取出cursor中的数据并显示
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
Log.e("********************Tag","id: "+id+" name: "+name);
}
}
public void queryAll(View view){
//1.得到ContentResolver对象
ContentResolver resolver = getContentResolver();
//2.调用其query 得到cursor
Uri uri = Uri.parse("content://com.example.mycontentprovider/person");
Cursor cursor = resolver.query(uri, null, null, null, null);
//3.取出cursor中的数据并显示
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
Log.e("********************Tag","id: "+id+" name: "+name);
}
}
public void insert(View view){
//1.得到ContentResolver对象
ContentResolver resolver = getContentResolver();
//2.调用其insert
Uri uri = Uri.parse("content://com.example.mycontentprovider/person/");
ContentValues values = new ContentValues();
values.put("name","jac");
resolver.insert(uri,values);
}
public void update(View view){
//1.得到ContentResolver对象
ContentResolver resolver = getContentResolver();
//2.调用其insert
Uri uri = Uri.parse("content://com.example.mycontentprovider/person/4"); //现在id是4
ContentValues values = new ContentValues();
values.put("name","Tom1");
int updateCount = resolver.update(uri, values, null, null);
Toast.makeText(this, "影响的行数"+updateCount+"", Toast.LENGTH_SHORT).show();
}
public void delete(View view){
//1.得到ContentResolver对象
ContentResolver resolver = getContentResolver();
//2.调用其insert
Uri uri = Uri.parse("content://com.example.mycontentprovider/person/5");
int deleteCount = resolver.delete(uri, null, null);
Toast.makeText(this, "影响的行数"+deleteCount+"", Toast.LENGTH_SHORT).show();
}
}