android developer tiny share-20170228

来源:互联网 发布:java编程教学视频 编辑:程序博客网 时间:2024/05/21 09:29

今天继续讲android的Content Provider的基础知识。今天会讲“访问提供程序”,讲ContentResolver和ContentProvider两个类,他们具有同名的方法,如query。另外,会对比query方法和SQL查询。

以下是android developer官方的讲解:


访问提供程序


应用从具有 ContentResolver 客户端对象的内容提供程序访问数据。 此对象具有调用提供程序对象(ContentProvider 的某个具体子类的实例)中同名方法的方法。 ContentResolver 方法可提供持续存储的基本“CRUD”(创建、检索、更新和删除)功能。

客户端应用进程中的 ContentResolver 对象和拥有提供程序的应用中的 ContentProvider 对象可自动处理跨进程通信。 ContentProvider 还可充当其数据存储区和表格形式的数据外部显示之间的抽象层。

注:要访问提供程序,您的应用通常需要在其清单文件中请求特定权限。 内容提供程序权限部分详细介绍了此内容。

例如,要从用户字典提供程序中获取字词及其语言区域的列表,则需调用 ContentResolver.query()。 query() 方法会调用用户字典提供程序所定义的 ContentProvider.query() 方法。 以下代码行显示了 ContentResolver.query() 调用:

// Queries the user dictionary and returns resultsmCursor = getContentResolver().query(    UserDictionary.Words.CONTENT_URI,   // The content URI of the words table    mProjection,                        // The columns to return for each row    mSelectionClause                    // Selection criteria    mSelectionArgs,                     // Selection criteria    mSortOrder);                        // The sort order for the returned rows

表 2 显示了 query(Uri,projection,selection,selectionArgs,sortOrder) 的参数如何匹配 SQL SELECT 语句:

表 2. Query() 与 SQL 查询对比。

query() 参数SELECT 关键字/参数说明UriFROM table_nameUri 映射至提供程序中名为 table_name 的表。projectioncol,col,col,...projection 是应该为检索到的每个行包含的列的数组。selectionWHERE col = valueselection 会指定选择行的条件。selectionArgs(没有完全等效项。选择参数会替换选择子句中 ? 占位符。) sortOrderORDER BY col,col,...sortOrder 指定行在返回的 Cursor 中的显示顺序。

0 0
原创粉丝点击