ContentProvider Learning Summary

Source: Internet
Author: User
ContentProvider Learning Summary

Data storage operations are essential for an Android application. Therefore, the Android system provides five data storage methods: SharedPreferences, SQLite, Content Provider, File, and network storage. Because data in Android systems is basically private, it is generally stored in its own "data/package name" directory and data is shared among different applications, the common method is to use Content Provider.

      SQLite: SQLite is a lightweight database that supports basic SQL syntax and is a common data storage method. Android provides a class named SQLiteDatabase for this database, which encapsulates APIs for database operations.

SharedPreferences: A common data storage method is used to store simple data configuration parameters. In essence, it is an xml file that stores key-value pairs of data, its storage location is in the/data/<package name>/shared_prefs directory. The SharedPreferences object can only obtain data, but does not support storage and modification. The Storage Modification is implemented through the Editor object.

File :  That is to say, the file (I/O) storage method is often used to store a large amount of data, but the disadvantage is that it is difficult to update data. In Android, the following File storage methods are usually used: Context. openFileOutput (String fileName, int mode) and Context. openFileInput (String fileName), which are operated through data streams. Network Storage:Network support is required for storing data on the network through Android network data packets.

The above four storage operations are generally aimed at the application data. If you want to operate on the data of other applications, ContentProvider will be a good choice. ContentProvider provides a unified interface for data storage and reading so that applications can share data. Many of the Android built-in data is in the ContentProvider format for developers to call (such as video, audio, images, address book, etc ). The advantage of using ContentProvider is that developers do not need to consider how data is stored internally. For example, if we want to use ContenProvider to store data, you only need to tell the insert function the uri of the ContentProvider and the data you want to store (including the column name and value). The query is the same. You only need to enter the Uri and the query table, column name, and query conditions, we do not need to know how to perform these operations in ContentProvider.

ContentProvider uses the Uri class for matching and association. For example, Uri uri = Uri. parse ("content: // com. changcheng. provider. contactprovider/contact "). The query strings used in ContentProvider are different from standard SQL queries. For many operations such as select, add, delete, and modify, we use a special URI, which is a common resource identifier and represents the data to be operated, every resource (such as text, image, and video) in Android can be represented by Uri. The Uri in Android consists of the following three parts: "content: //" (authory), data path, and resource ID (optional ). If an ID exists, it indicates a specific resource. If no ID exists, it indicates the whole of the path. The following is an example Uri:

Content: // media/internal/imagesThisURIReturns all images stored on the device.
Content: // contacts/people/
ThisURIAll contacts on the device will be returned.
Content: // contacts/people/45 
ThisURIReturns a single result (in the contact informationIDIs45Contact Records)

When ContentProvider is used to share data in different applications, the data exposure method is similar to that of tables in the database. ContentResolver uses a database-like method to access data from ContentProvider. It queries the data provided in ContentProvider through Uri. During the query, you also need to know the name of the target database, the data type or resource ID of the data segment.

The following is a newContentProviderComplete process:

  1. Create a class that inherits the ContentProvider parent class and implement basic query (), insert (), update (), delete (), and other methods, these methods are also the data operation methods exposed to other applications.
  2. Use the <provider> label in AndroidManifest. xml of your own application to set and define the implemented ContentProvider.
  3. Define a class variable named CONTENT_URI whose type is public static final. You must specify a unique string value for it. The best solution is to use the full name of the class, this is also the unique identifier for your ContentProvider query. This definition corresponds to your AndroidManifest. android: authorities ="Downloads"Is consistent.
  4. Defines the name of the data column you want to return to the client. This variable is a specific field for external applications to operate on your data. It is generally consistent with the name of the variable that stores data locally, this makes it easy to understand the call. If you are using the Android database, you must define a column named _ id for it to indicate the uniqueness of each record.
  5. Create your data storage system. Most contentproviders use the Android file system or SQLite database to maintain data, but you can also store data in any way you want. The actual storage location of the data lies in this. ContentProvider only encapsulates the data and specifies the specific steps for local data storage when implementing the ContentProvider interface method. .
  6. If you want to store byte data, such as bitmap files, the data column is actually a URI string that represents the actual file to be saved and read the corresponding file data through it. To process this data type, the Content Provider needs to implement a field named _ data, and the _ data field lists the precise paths of the file on the Android file system. This field is not only used by the client, but also for ContentResolver. The client can call ContentResolver. the openOutputStream () method is used to process the file resources pointed to by the URI. If it is ContentResolver itself, it can directly access the data file because it has higher permissions than the client.
  7. The query returns a Cursor type object. All write operations, such as insert (), update (), and delete (), will be monitored. We can use the ContentResover (). Policychange () method to notify the listener about data updates.

 

 

The following is aboutDownloadProviderBrief introduction:

  1. DownloadManager is the system's operation management class for DownloadProvider. This class defines related download operations and further encapsulates data operations for external calls. Path:

Frameworks \ base \ core \ java \ android \ app \ DownloadManager. java

The browser first calls the DownloadManager's enqueue function to download the file. The enqueue function mainly splits the Request instance sent by the user into a ContentValues instance and then executes ContentResolver. insert (Downloads. impl. ALL_DOWNLOADS_CONTENT_URI, values). This function is the insert function implemented by DownloadProvider. It adds the download information to the database, and finally the enqueue function returns the ID returned after inserting this data.

  1. The Downloads class defines the related fields of the external operation DownloadProvider. Path:

Frameworks \ base \ core \ java \ android \ provider \ Downloads. java

This class defines the related field variables of DownloadProvider, for example: Uri ALL_DOWNLOADS_CONTENT_URI = Uri. parse ("content: // downloads/all_downloads") defines its unique address, so that external applications can directly call this Uri through ContentResolver to find data in DownloadProvider; string _ DATA = "_ data" defines the column fields corresponding to the download file storage path; COLUMN_MIME_TYPE = "mimetype" defines the download file type, etc.

3. the DownloadProvider is located in the packages \ providers \ DownloadProvider path and inherits the basic data operation functions delete (), query (), insert (), update (), getType (), and so on to override ContentProvider, external applications can use ContentResolver to operate these functions to facilitate data sharing between different applications. DatabaseHelper is an internal class that inherits SQLiteOpenHelper. The database created here (DB_NAME = "downloads. db ") is the location where DownloadProvider stores data, and the table (DB_TABLE =" downloads ") in the database is the detailed list of all downloaded files.

 

ContentProvider Learning Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.