zoukankan      html  css  js  c++  java
  • 3.App Components-Content Providers

    1. Content Providers

      A content provider manages access to a central repository of data.

      A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are

        primarily intended to be used by other applications, which access the provider using a provider client object. Together, providers and

          provider clients offer a consistent, standard interface to data that also handles inter-process communication and secure data access.

    2. OverView

      2.1 Accessing a provider

        An application accesses the data from a content provider with a ContentResolver client object. This object has methods that call

          identically-named methods in the provider object, an instance of one of the concrete subclasses of ContentProvider. The

          ContentResolver methods provide the basic "CRUD" (create, retrieve, update, and delete) functions of persistent storage.

      For example, read contrats

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
                    null, 
                    null, 
                    null, 
                    null);
            while(cursor.moveToNext()){  //cursor is like a point to every element
                Log.d("Contract Name",cursor.getString(cursor.getColumnIndex((ContactsContract.Contacts.DISPLAY_NAME))));
            }

      2.2 Content URIs

        A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority)

          and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the

          table is one of the arguments(参数).

        In the previous lines of code, the full URI for the "words" table is:

         content://user_dictionary/words 

        where the user_dictionary string is the provider's authority,

        and words string is the table's path.

        The string content:// (the scheme) is always present, and identifies this as a content URI, like the http:// indentifies HTTP internet

        Many providers allow you to access a single row in a table by appending an ID value to the end of the URI. For example, to retrieve a

          row whose _ID is 4 from user dictionary, you can use this content URI:

         Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4); 

    3. Retrieving Data from the Provider

      To retrieve data from a provider, follow these basic steps:

        <1> Request the read access permission for the provider.

        <2> Define the code that sends a query to the provider

      3.1 Requesting read access Permission

        To retrieve data from a provider, your application needs "read access permission" for the provider

      3.2 Constructing the query

        

      3.3 Displaying qurey results

      3.4 Getting data from qurey results

  • 相关阅读:
    JAXB注解 @XmlRootElement 及XML文件解析详解
    JAXB 实现java对象与xml之间互相转换
    MyBatis之ResultMap标签
    如何通过<include/>标签重用Mybatis的代码段
    @Component, @Repository, @Service的区别
    @repository的含义,并且有时候却不用写,为什么?
    浪潮启动元脑生态计划,聚焦计算机视觉、量化交易等基础应用
    初生牛犊不怕虎 造车新势力的硬核移动互联科技盘点
    注意,有场景的公司正在拿起AI武器
    机器人运动大赛8月落地嘉峪关,编程和军事成亮点
  • 原文地址:https://www.cnblogs.com/iMirror/p/4059688.html
Copyright © 2011-2022 走看看