ContentResolver的基本用法

Android数据交互详解
本文介绍了Android系统中ContentResolver的作用及使用方法。通过示例代码展示了如何利用ContentResolver查询应用程序暴露的数据,包括创建Uri、获取ContentResolver实例、执行查询操作等步骤。
部署运行你感兴趣的模型镜像

在Android系统中,ContentResolver充当着桥梁的角色。应用程序通过ContentProvider暴露自己的数据,通过ContentResolver对应用程序暴露的数据进行操作。

  • 通过ContentResovler对象实现数据的操作代码如下:

Uri uri=Uri.parse(“content://cn.itcast.db.personprovider/person”);
ContentResolver resolver=context.getContentResolver();
Cursor cursor = resolver . query ( un , new String ( " address " , " date " , " type " , " body " ) , null , null , null )
while(cursor.moveToNext()){
String address=cursor.getString(0);

}
cursor.close():在这里插入图片描述
contentobserver常用方法
在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### ContentResolver 的使用方法 `ContentResolver` 是 Android 中用于访问 `ContentProvider` 提供的数据的核心工具。它允许应用程序之间共享数据,而无需直接操作数据库或其他存储方式。 #### 获取 ContentResolver 实例 可以通过调用上下文对象的 `getContentResolver()` 方法获得一个 `ContentResolver` 对象实例[^2]。通常情况下,这会在 Activity 或 Application 上下文中完成: ```java ContentResolver resolver = context.getContentResolver(); ``` #### 查询数据 (Query) 要查询由某个 `ContentProvider` 提供的数据,可以使用 `query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)` 方法。以下是其参数含义: - **uri**: 数据源的 URI 地址。 - **projection**: 要返回的列名数组。 - **selection**: WHERE 子句条件字符串。 - **selectionArgs**: 替代 WHERE 条件中的占位符 "?" 的值。 - **sortOrder**: 排序顺序。 示例代码如下: ```java Uri contactUri = ContactsContract.Contacts.CONTENT_URI; String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME}; Cursor cursor = resolver.query(contactUri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { do { long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Log.d("Contact", "ID: " + id + ", Name: " + name); } while (cursor.moveToNext()); } if (cursor != null) { cursor.close(); } ``` #### 插入数据 (Insert) 如果需要向 `ContentProvider` 添加新数据,则可使用 `insert(Uri uri, ContentValues values)` 方法。例如插入一条新的联系人记录: ```java ContentValues contentValues = new ContentValues(); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "John Doe"); Uri insertUri = resolver.insert(ContactsContract.Data.CONTENT_URI, contentValues); Log.d("Insert", "Inserted Contact Uri: " + insertUri.toString()); ``` #### 更新数据 (Update) 更新现有数据可通过 `update(Uri uri, ContentValues values, String whereClause, String[] whereArgs)` 完成。下面是一个例子展示如何修改特定字段的内容: ```java ContentValues updateValues = new ContentValues(); updateValues.put(ContactsContract.Contacts.DISPLAY_NAME, "Jane Smith"); int rowsAffected = resolver.update(contactUri, updateValues, "_id=?", new String[]{"1"}); Log.d("Update", "Rows Affected: " + rowsAffected); ``` #### 删除数据 (Delete) 当不再需要某些数据时,可以用 `delete(Uri uri, String whereClause, String[] whereArgs)` 将它们移除。比如删除指定 ID 的项: ```java int deletedCount = resolver.delete(contactUri, "_id=?", new String[]{"1"}); Log.d("Delete", "Deleted Count: " + deletedCount); ``` 以上就是 `ContentResolver` 基本的操作流程及其主要 API 函数介绍[^3]。 ### 注意事项 为了能够成功执行这些 CRUD 操作,可能还需要申请相应的权限声明于 AndroidManifest 文件之中,并动态请求运行时许可[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值