当前位置:操作系统 > 安卓/Android >>

Android内容提供者——Content Providers(一)

Content Providers是Android四大组件之一,扮演者非常重要的角色,看下官方文档对它的解释:

Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard inte易做图ce that connects data in one process with code running in another process.

可以看到Content providers负责管理结构化数据的访问,Content providers封装数据并且提供一套定义数据安全的机制。Content providers是一套在不同进程间进行数据访问的接口。
Content providers为数据跨进程访问提供了一套安全的访问机制,对数据组织和安全访问提供了可靠的保证。

另外来看看官方文档是如何解释Content Providers的使用的:
When you want to access data in a content provider, you use theContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolverobject communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.

意思是当你想要通过Content Providers访问数据时,在应用程序的上下文(Context)中使用ContentResolver对象最为客户端(client)与provider进行交互。ContentResolver对象通过实现抽象类ContentProvider的一个实例来访问provider。Provider对象从客户端(client)接收数据请求,执行请求操作并且返回请求结果。

Android通过Content Provider来管理数据诸如音频、视频、图片和通讯录等。还可以通过ContentProvider来访问SQLite数据库等,下面来看看Content Provider的基本使用。

在此之前,官方文档中给出了一些是否需要需用Content Providers的建议:
Decide if you need a content provider. You need to build a content provider if you want to provide one or more of the following features:
You want to offer complex data or files to other applications.
You want to allow users to copy complex data from your app into other apps.
You want to provide custom search suggestions using the search framework.

在以下情况下你需要使用Content Providers:
1.你想为其他应用程序提供复杂数据或文件;
2.你想允许用户从你的应用程序中拷贝复杂数据到其他的应用中
3.你想使用搜索框架提供自定义的查询建议功能

Content Provider通过URI(统一资源定位符)来访问数据,URI可以理解为访问数据的唯一地址,URI由authority和数据地址构成,关于authority可以理解成网站地址中的主机地址,而数据地址可以理解成某一个页面的子地址,二者共同构成了一个完整的访问地址,关于authority的命名,官方文档中有这么一句话 If your Android package name iscom.example.<appname>, you should give your provider the authority com.example.<appname>.provider.可见对于authority的命名还是有一定的规范性的。
关于URI的格式:content://<authority>/<path>/<id>,path就是数据路径,比如说一张表,而id就是这张表中主键为id的一行,也可以理解成一个实体对象。

看下构建Content Provider和使用URI的代码:
[java] 
publicclassExampleProviderextendsContentProvider{... 
    // Creates a UriMatcher object. 
    privatestaticfinalUriMatcher sUriMatcher;... 
    /*
     * The calls to addURI() go here, for all of the content URI patterns that the provider
     * should recognize. For this snippet, only the calls for table 3 are shown.
     */... 
    /*
     * Sets the integer value for multiple rows in table 3 to 1. Notice that no wildcard is used
     * in the path
     */ 
    sUriMatcher.addURI("com.example.app.provider","table3",1); 
 
    /*
     * Sets the code for a single row to 2. In this case, the "#" wildcard is
     * used. "content://com.example.app.provider/table3/3" matches, but
     * "content://com.example.app.provider/table3 doesn't.
   * 这里说明了两种通配符的作用
   * *: Matches a string of any valid characters of any length.
   * #: Matches a string of numeric characters of any length.
 
     */ 
    sUriMatcher.addURI("com.example.app.provider","table3/#",2);... 
    // Implements ContentProvider.query() 
    publicCursor query( 
        Uri uri, 
        String[] projection, 
        String selection, 
        String[] selectionArgs, 
        String sortOrder){... 
        /*
         * Choose the table to query and a sort order based on the code returned for the incoming
         * URI. Here, too, only the statements for table 3 are shown.
         */ 
        switch(sUriMatcher.match(uri)){ 
 
 
            // If the incoming URI was for all of table3 
            case1: 
 
                if(TextUtils.isEmpty(sortOrder)) sortOrder ="_ID ASC"; 
                break; 
 
            // If the incoming URI was for a single row 
            case2: 
 
                /*
                 * Because this URI was for a single row, the _ID value part is
                 * present. Get the last path segment from the URI; this is the _ID value.
                 * Then, append the value to the WHERE clause for the query
                 */ 
                selection = selection +"_ID = " uri.getLastPathSegment(); 
                break; 
 
            default: 
            ... 
                // If the URI is not recognized, you should do some error handling here. 
        } 
        // call the code to actually do the query } 

实现抽象类ContentProvider后,有几个需要实现的方法:
query()
Retrieve data from your provider. Use the arguments to select the table to query, the rows and columns to return, and the sort order of the result. Return the data as a Cursor object.
inser

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,