1 Star 0 Fork 0

lolik / ContentProvider

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

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:调用的表。table1
  • com.example.app包名
  • providerContentProvider 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);
            }
        );
    }
}

结果截图

左边提供者,右边接收者

git仓库

https://gitee.com/qwe2412322029/contentprovider

空文件

简介

安卓内容提供者 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/qwe2412322029/contentprovider.git
git@gitee.com:qwe2412322029/contentprovider.git
qwe2412322029
contentprovider
ContentProvider
master

搜索帮助