ManagedQuery在Android中已被弃用,推荐使用ContentResolver来代替,以下是使用ContentResolver进行查询的示例:
- 获取ContentResolver对象:
ContentResolver contentResolver = getContentResolver();
- 构建Uri对象,表示要查询的内容URI:
Uri uri = Uri.parse("content://com.example.provider/book");
- 定义要查询的列:
String[] projection = {"_id", "name", "author"};
- 执行查询操作:
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
- 遍历查询结果:
if (cursor != null) { while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); // 处理查询结果 } }
- 关闭Cursor:
if (cursor != null) { cursor.close(); }
请注意,这只是一个基本示例,具体的查询条件和操作根据实际需求进行调整。