[Summary of Four Components - ContentProvider]

ContentProvider

 进行跨进程通信,实现进程间得数据交互和共享。通过 Context 中 getContentResolver() 获得实例,通过 Uri 匹配进行数据的增删改查。ContentProvider 使用表的形式来组织数据,无论数据的来源是什么,ContentProvider 都会认为是一种表,然后把数据组织成表格

1. Summary

  1. It provides us with a mechanism for sharing data between applications and provides a unified interface for storing and retrieving data.

  2. And ContentProvide encapsulates the data, so you don't need to care about the details of data storage. [Use tables to organize data].

  3. Use ContentProvider to share data between different applications.

  4. Android provides a default ContentProvider for some common data (including audio, video, pictures and contacts, etc.). In general, the advantage of using ContentProvider to share data externally is to unify the access method of data.

  5. ContentProvider is divided into system and custom, the system is for example: contact, picture, video, audio and other data.

  6. [SQLite is used to store data private to an application, while ContentProvider provides a mechanism for sharing data between applications]

  7. [When an application inherits the ContentProvider class and rewrites the methods used to provide data and manipulate stored data, it can share its data with other applications.

2. [Association between ContentProvider and Uniform Resource Identifier (url)]

  1. Each ContentProvider has a public URI, which is used to represent the data provided by this ContentProvider.

  2. All ContentProviders provided by the Android system are stored in the android.provider package]

  3. Learning the implementation of ContentProvider is actually to make it easier to use the ContentProvider provided by the system in the future, because it is rarely necessary to create a ContentProvider by yourself in actual development]

3. The method provided by ContentProvider:

query: query, returns a Cursor object, Cursor itself is an interface, but Android has different implementations of Cursor objects for different database access insert:
   insert
   update: update
   delete: delete
   getType: get data type
   onCreate: called when creating data Callback

Then we can override these methods in our own subclasses.

4. [Custom ContentProvider - Example]

  1. Define a constant class: which must contain: public static final CONTENT_URI. Used to represent this ContentProvider, the data URI provided

     一个ContentProvider如果有多个子表,则需要定义多个URI常量,且建议定义一些常量来表示 表中的列名。并设置好 主键()
    
  2. Define a class that inherits from ContentProvider

  3. Then rewrite the parent class: query, insert, update, delete, getType, onCreate, and other methods.

  4. Declared in the AndroidManifest.xml file

Guess you like

Origin blog.csdn.net/UserFrank/article/details/129204618