探究ContentProvider(一)

探究ContentProvider(一)

1. 简介

ContentProvider是Android官方推荐的进行跨程序数据共享的技术,相比较Sharedpreferences和文件读写更加安全、可靠。常见使用ContentProvider技术进行跨程序数据共享的信息包括通讯录、手机音频、短信内容等。ContentProvider可以选择共享部分数据,而不必要全部共享。

2. 使用场景

1、通过ContentProvider读取或操作相应程序提供的数据。

2、自定义ContentProvider,给自己程序中的数据提供外部访问接口。

3. ContentResolver

  1. 简介

实现读取以及增删改ContentProvider提供的数据,需要解决ContentResolver

  1. 程序中获取ContentResolver

程序中通过Context#getContentResolver方法获取ContentResolver

ContentResolver中包含insert、delete、update、query方法,分别对应执行数据的增删改查操作

4. URI

4.1. URI是什么

类似于SQLite,ContentProvider需要管理数据,SQLite中使用的是数据库和数据表,一个表中数据就是某一类的数据,而ContentProvider使用的是程序包名+路径(path),这两者组合在一起就是一个URI;

4.2. URI的示例

URI分为authority+path部分,前者形式为包名.provider,后者形式为/table1,组合在一起为例如com.example.app.provider/table1;

ContentProvider使用的URI需要在URI的内容开头加上content://,以和别的URI进行区分,那么完整的URI内容为content://com.example.app.provider/table1;

4.3. 程序中使用URI

在程序中,使用URI需要把它转换成Uri对象,代码:

kotlin 复制代码
val uri: Uri = Uri.parse("content://com.example.app.provider/table1")

5. 操作ContentProvider数据

5.1. 查询(query)方法

kotlin 复制代码
fun query(
    uri: Uri,                    // 要查询的内容URI
    projection: Array<String>?,  // 要返回的列名数组
    selection: String?,          // WHERE条件子句
    selectionArgs: Array<String>?, // WHERE条件的参数值
    sortOrder: String?           // 排序方式
): Cursor?

参数说明:

uri: 内容提供者的唯一标识,决定查询哪个表或数据源

projection: 要返回的列名数组,null表示返回所有列

kotlin 复制代码
// 示例:只返回name和email列
val projection = arrayOf("name", "email")

selection: WHERE条件,不包含WHERE关键字

kotlin 复制代码
// 示例:查询age大于18的记录
val selection = "age > ? AND city = ?"

selectionArgs: WHERE条件的参数值,用于替换selection中的?

kotlin 复制代码
val selectionArgs = arrayOf("18", "Beijing")

sortOrder: 排序规则

kotlin 复制代码
val sortOrder = "name ASC, age DESC" // 按姓名升序,年龄降序

5.2. 插入(insert)方法

kotlin 复制代码
fun insert(uri: Uri, values: ContentValues?): Uri?

参数说明:

uri: 要插入数据的目标URI

values: 要插入的数据,使用ContentValues包装

kotlin 复制代码
val values = ContentValues().apply {
    put("name", "张三")
    put("age", 25)
    put("email", "zhangsan@example.com")
}

5.3. 更新(update)方法

kotlin 复制代码
fun update(
    uri: Uri,
    values: ContentValues?,
    selection: String?,
    selectionArgs: Array<String>?
): Int

参数说明:

uri: 要更新数据的目标URI(可以包含特定记录的ID)

values: 要更新的字段和值

selection: WHERE条件,确定更新哪些记录

selectionArgs: WHERE条件的参数值

5.4. 删除(delete)方法

kotlin 复制代码
fun delete(
   	uri: Uri,
   	selection: String?,
   	selectionArgs: Array<String>?
): Int

参数说明:

uri: 要删除数据的目标URI(可以包含特定记录的ID)

selection: WHERE条件,确定删除哪些记录

selectionArgs: WHERE条件的参数值

相关推荐
Kapaseker1 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴2 小时前
Android17 为什么重写 MessageQueue
android
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿2 天前
Android MediaPlayer 笔记
android
Jony_2 天前
Android 启动优化方案
android
阿巴斯甜2 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android