欢迎光临散文网 会员登陆 & 注册

Android开发学习教程(32)- Android ContentResolver使用教程

2023-01-30 01:11 作者:ChatGPT云炬学长  | 我要投稿

—— 星光不问赶路人,时光不负有心人。

ContentProvider 帮助android:属性与 ContentProvider ID 相同。在其他应用中搜索我的应用的Provider时需要知道provider

    android:name=".SampleContentProvider"
    android:authorities="example.<a href="https://yunjunet.cn/tag/contentprovidersample" contentprovidersample"target="_blank" style="color: rgb(102, 102, 102); cursor: pointer; transition: all 0.3s ease 0s;">contentprovidersample.provider"
    android:exported="true"
    android:<a href="https://yunjunet.cn/tag/permission" permission"target="_blank" style="color: rgb(102, 102, 102); cursor: pointer; transition: all 0.3s ease 0s;">permission="com.example.<a href="https://yunjunet.cn/tag/contentprovider" contentprovider"target="_blank" style="color: rgb(102, 102, 102); cursor: pointer; transition: all 0.3s ease 0s;">contentprovidersample.provider.READ_WRITE"
    android:readPermission="com.harvic.contentProviderBlog.READ"
    android:writePermission="com.harvic.cotentProviderBlog.WRITE"/>


android:permission:应用程序读、写 ContentProvider 中的数据所必需的权限名称。 本属性为一次性<a href="https://yunjunet.cn/tag/%e8%ae%be%e7%bd%ae" 设置"target="_blank" style="color: rgb(102, 102, 102); cursor: pointer; transition: all 0.3s ease 0s;">设置读和写权限提供了快捷途径。 不过,readPermission和writePermission属性优先于本设置。 如果同时设置了readPermission属性,则其将控制对 Content Provider 的读取。 如果设置了writePermission属性,则其也将控制对 Content Provider 数据的修改。也就是说如果只设置permission权限,那么拥有这个权限的应用就可以实现对这里的ContentProvider进行读写;如果同时设置了permission和readPermission那么具有readPermission权限的应用才可以读,拥有permission权限的才能写!也就是说只拥有permission权限是不能读的,因为readPermission的优先级要高于permission;如果同时设置了readPermission、writePermission、permission那么permission就无效了。

ContentProvider方法

ContentProvider 提供查询、插入、更新或删除等 API 以及数据库。如果您知道 Provider 的授权,您的应用可以使用 ContentProvider 访问 Provider。

查询

首先,您可以创建一个具有权限的 Uri,并将此 Uri 作为参数传递给 ContentResolver 以进行查询。查询API 的使用类似于 SQLite 等数据库。您可以设置查询等操作来找到您需要的数据。然后,您可以通过返回的 Cursor 对象读取数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
String table_name = "cheeses";
String authority = "com.example.contentprovidersample.provider";
Uri uri = Uri.parse("content://" + authority + "/" + table_name);
Cursor cursor = getContentResolver().query(
                    uri,  // uri
                    null// projection
                    null// selection
                    null// selectionArgs
                    "id" // sortOrder
                );
if(cursor!=null)
{
    cursor.moveToFirst();
    do{
        int idIndex= cursor.getColumnIndex("id");
        int id= cursor.getInt(idIndex);
        int nameIndex= cursor.getColumnIndex("name");
        String name= cursor.getString(nameIndex);
    }while(cursor.moveToNext());
}

一个Uri由以下几部分组成:

1
2
3
4
Authority:授权信息,用以区别不同的ContentProvider;
Path:表名,用以区分ContentProvider中不同的数据表;
Id:Id号,用以区别表中的不同数据;

如以上例子中的URI_CHEESE就表示cheeses这张表,如果改成

1
2
3
int ID = 5;
Uri URI_CHEESE_ITEM = Uri.parse("content://" + authority + "/" + table_name + "/" + ID);

就表示cheeses这张表中的id为5的这条数据。所以上面例子就表示使用query方法查询cheeses这张表并返回游标Cursor,query方法有五个参数:

1
2
3
4
5
6
第一个参数uri:可以理解为作为标识是操作哪个ContentProvider,uri也可以理解为是ContentProvider的具体"路径",如上面例子中的"路径"就表示是cheeses表;
第二个参数projection:查询cheeses表哪几列数据,null表示查询所有列;
第三个参数selection:查询条件,建议使用占位符,为null是没有查询条件,即查询全部;(什么是占位符?见上一篇文章SQLite使用教程)
第四个参数selectionArgs:查询条件的值,与第三个参数selection搭配使用;
第五个参数sortOrder:查询按某列排序,如上面例子中是按ID升序排列,也可以写成"id ASC",降序就是"id DESC"

插入

1
2
3
4
5
6
Uri contentUri = Uri.parse("content://" + authority + "/" + table_name);
ContentValues contentValues = new ContentValues();
contentValues.put("name""张三");
contentValues.put("email""123@qq.com");
getContentResolver().insert(contentUri, contentValues);

更新

1
2
3
4
5
6
7
8
Uri contentUri = Uri.parse("content://" + authority + "/" + table_name);
ContentValues contentValues = new ContentValues();
contentValues.put("name""张三");
contentValues.put("email""123@qq.com");
String whereClause = "id = ?";
String placeHolderValueArr[] = {"1"};
getContentResolver().update(contentUri, contentValues, whereClause , placeHolderValueArr);

删除

1
2
3
4
5
Uri contentUri = Uri.parse("content://" + authority + "/" + table_name);
String whereClause = "id = ?";
String placeHolderValueArr[] = {"1"}
getContentResolver().delete(contentUri, whereClause , placeHolderValueArr);

异步操作

在Activity和Fragment中可以使用LoaderManager和CursorLoader异步访问ContentProvider。当LoaderManager获取到Provider的所有数据后,会通过LoaderCallbacks进行回调。当 Provider 数据由于添加、删除、更新等发生变化时,它也会回调。


Android开发学习教程(32)- Android ContentResolver使用教程的评论 (共 条)

分享到微博请遵守国家法律