Alternative Forms of Provider Access

  Three alternative forms of provider access are important in application development:

  • Batch access: You can create a batch of access calls with methods in the ContentProviderOperation class, and then apply them with ContentResolver.applyBatch().

    批处理式访问
  • Asynchronous queries: You should do queries in a separate thread. One way to do this is to use a CursorLoader object. The examples in the Loaders guide demonstrate how to do this.
    异步访问,通过CursorLoader在其它线程中查询。本文不介绍这种方式,在Loaders中有示例。
  • Data access via intents: Although you can't send an intent directly to a provider, you can send an intent to the provider's application, which is usually the best-equipped to modify the provider's data.
    通过intent间接访问。

  Batch access and modification via intents are described in the following sections.

Batch access 批处理

  Batch access to a provider is useful for inserting a large number of rows, or for inserting rows in multiple tables in the same method call, or in general for performing a set of operations across process boundaries as a transaction (an atomic operation).

  To access a provider in "batch mode", you create an array of ContentProviderOperation objects and then dispatch them to a content provider with ContentResolver.applyBatch(). You pass the content provider's authority to this method, rather than a particular content URI. This allows each ContentProviderOperation object in the array to work against a different table. A call to ContentResolver.applyBatch() returns an array of results.

  批处理方式使用的类是ContentProviderOperation。ContactAdder.java中有示例代码。

  The description of the ContactsContract.RawContacts contract class includes a code snippet that demonstrates batch insertion. The Contact Manager sample application contains an example of batch access in its ContactAdder.java source file.

Data access via intents 通过intent方式间接访问

  Intents can provide indirect access to a content provider. You allow the user to access data in a provider even if your application doesn't have access permissions, either by getting a result intent back from an application that has permissions, or by activating an application that has permissions and letting the user do work in it.

  一个有权限的应用访问provider,然后用intent与这个应用交互来实现间接访问provider.

Getting access with temporary permissions 申请临时URI权限 然后访问

  You can access data in a content provider, even if you don't have the proper access permissions, by sending an intent to an application that does have the permissions and receiving back a result intent containing "URI" permissions. These are permissions for a specific content URI that last until the activity that receives them is finished. The application that has permanent permissions grants temporary permissions by setting a flag in the result intent:

  有权限的应用可以在intent中设置下面两个标记然后返回,包含了临时URI权限。
  • Read permission: FLAG_GRANT_READ_URI_PERMISSION
  • Write permission: FLAG_GRANT_WRITE_URI_PERMISSION

  Note: These flags don't give general read or write access to the provider whose authority is contained in the content URI. The access is only for the URI itself.

  注意:这个临时权限只是当前这个URI的,并不是整个数据库的。

  A provider defines URI permissions for content URIs in its manifest, using the android:grantUriPermission attribute of the <provider> element, as well as the <grant-uri-permission> child element of the <provider> element. The URI permissions mechanism is explained in more detail in the Security and Permissions guide, in the section "URI Permissions".

  在 <provider android:grantUriPermission >...和它的子元素 <grant-uri-permission> 可以声明一个URI权限。

  For example, you can retrieve data for a contact in the Contacts Provider, even if you don't have the READ_CONTACTS permission. You might want to do this in an application that sends e-greetings to a contact on his or her birthday. Instead of requesting READ_CONTACTS, which gives you access to all of the user's contacts and all of their information, you prefer to let the user control which contacts are used by your application. To do this, you use the following process:

  下面是一个没有Contact Provider访问权限的应用通过申请临时URI权限访问Contact Provider的步骤:
  1. Your application sends an intent containing the action ACTION_PICK and the "contacts" MIME type CONTENT_ITEM_TYPE, using the method startActivityForResult().

    在intent中设置ACTION_PICK和 CONTENT_ITEM_TYPE ,然后调用startActivityForResult()启动People app's "selection" activity.
  2. Because this intent matches the intent filter for the People app's "selection" activity, the activity will come to the foreground.
    People app's "selection" activity到前台。
  3. In the selection activity, the user selects a contact to update. When this happens, the selection activity calls setResult(resultcode, intent) to set up a intent to give back to your application. The intent contains the content URI of the contact the user selected, and the "extras" flags FLAG_GRANT_READ_URI_PERMISSION. These flags grant URI permission to your app to read data for the contact pointed to by the content URI. The selection activity then calls finish() to return control to your application.
    "selection" activity会在返回 Intent中添加临时URI权限 FLAG_GRANT_READ_URI_PERMISSION,然后它自己调用finish()
  4. Your activity returns to the foreground, and the system calls your activity's onActivityResult() method. This method receives the result intent created by the selection activity in the People app.
    你的应用收到由"selection" activity返回的带有临朝权限的intent
  5. With the content URI from the result intent, you can read the contact's data from the Contacts Provider, even though you didn't request permanent read access permission to the provider in your manifest. You can then get the contact's birthday information or his or her email address and then send the e-greeting.
    利用这个临时权限访问Contact Provider.

Using another application 启动有访问权限的第3方应用

  A simple way to allow the user to modify data to which you don't have access permissions is to activate an application that has permissions and let the user do the work there.

  For example, the Calendar application accepts an ACTION_INSERT intent, which allows you to activate the application's insert UI. You can pass "extras" data in this intent, which the application uses to pre-populate the UI. Because recurring events have a complex syntax, the preferred way of inserting events into the Calendar Provider is to activate the Calendar app with an ACTION_INSERT and then let the user insert the event there.

Displaying data using a helper app

  If your application does have access permissions, you still may want to use an intent to display data in another application. For example, the Calendar application accepts an ACTION_VIEW intent, which displays a particular date or event. This allows you to display calendar information without having to create your own UI. To learn more about this feature, see the Calendar Provider guide.

  The application to which you send the intent doesn't have to be the application associated with the provider. For example, you can retrieve a contact from the Contact Provider, then send an ACTION_VIEW intent containing the content URI for the contact's image to an image viewer.

ContentProvider官方教程(7)3种访问形式:批处理、异步访问、intent间接访问(临时URI权限)的更多相关文章

  1. ContentProvider官方教程(9)定义一个provider完整示例:实现方法,定义权限等

    Creating a Content Provider In this document Designing Data Storage Designing Content URIs Implement ...

  2. ContentProvider官方教程(3)ContentResolver查询、遍历 示例

    Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...

  3. ContentProvider官方教程(2)简介、Content URIs

    In this document Overview Accessing a provider Content URIs Content Provider Basics A content provid ...

  4. ContentProvider官方教程(10)<provider>元素及属性介绍

    The <provider> Element Like Activity and Service components, a subclass of ContentProvider mus ...

  5. ContentProvider官方教程(5)ContentResolver插入、更新、删除 示例

    Inserting, Updating, and Deleting Data In the same way that you retrieve data from a provider, you a ...

  6. ContentProvider官方教程(4)ContentResolver权限

    Content Provider Permissions A provider's application can specify permissions that other application ...

  7. ContentProvider官方教程(1)何时用content provider

    Content Providers Content providers manage access to a structured set of data. They encapsulate the ...

  8. ContentProvider官方教程(11)Calendar Provider、Contacts Provider、Storage Access Framework

    Calendar Provider: guide/topics/providers/calendar-provider.html Contacts Provider: guide/topics/pro ...

  9. ContentProvider官方教程(8)自定义MIME

    MIME Type Reference Content providers can return standard MIME media types, or custom MIME type stri ...

随机推荐

  1. 关于 static 的用途

    1.三个作用 第一个作用是 隐藏 输出: Hello 所有未加static前缀的全局变量和函数都具有全局可见性,其它的源文件也能访问.此例中,a是全局变量,msg是函数,并且都没有加static前缀, ...

  2. oracle冷备份后恢复

    本地恢复 在运行中输入cmd. 在cmd界面中输入sqlplus/nolog进入sql*plus. 以dba身份连接数据库conn sys/你设定的密码 as sysdba. 输入:shutdown ...

  3. PHP自动生成后台导航网址的最佳方法

    'http://www.jbxue.com'=> '脚本学堂首页', </script>

  4. Windows系统文件受损的修复技巧

    Windows2000/XP系统文件受损的修复技巧 意外重启.安装了不兼容的软件.恶意程序侵扰.误删文件……有太多种可能性会使我们的系统文件受损,而系统文件受损后最直接的表现就是系统不稳定.经常出现错 ...

  5. android 百度地图定位开发1

    首先注册成为百度开发者 然后进入百度开发者中心 点击LBS 跳到下一个页面 点击Android 开发 里面的基础地图 进入  点击获取密钥 进入   点击创建应用 进入     应用名称自己填 应用类 ...

  6. linux设备树笔记__dts基本概念及语法【转】

    转自:http://www.360doc.com/content/15/1113/11/15700426_512794532.shtml 设备树手册(Device Tree Usage)原文地址:ht ...

  7. The reference to entity “idNo” must end with the ';' delimiter 异常处理

    解决方案很简单,就是把配置项值中用到"&"的地方改成"&".原因是sax解析的类库在读取文件的时候是根据转义后的格式进行读取的,遇到" ...

  8. dirname和basename命令

    dirname返回文件所在目录路径,而basename则相反,去掉路径返回最后的文件名. dirname指令 1.功能:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目 ...

  9. 系统图片uri的问题

    调用系统图库会出现两种uri的问题,一个是在文件管理器中的图库中,获取到的地址为:content://media/external/images/media/972  这种格式 另外一种的是系统文件管 ...

  10. [C++]C++标准里 string和wstring

    typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; 前者string是常用类型, ...