Data storage of ContentProvider in Android development tutorial

1、 Introduction to saving data by ContentProvider

A program can completely expose its own data by implementing an abstract interface of ContentProvider, and ContentProvider exposes the data in a way similar to the table in the database. Then the external access to the data provided by it should be basically the same as the operation of obtaining data from the database, except that the URL is used to represent the "database" that the external needs to access.

ContentProvider provides a way to share data among multiple applications.

ContentProvider is a class that implements a set of standard methods for providing access to data by other applications. The application can perform the following operations in the ContentProvider: query data, modify data, add data and delete data.

Standard ContentProvider: Android provides some standard ContentProviders that have been implemented in the system, such as contact information, picture library, etc. These ContentProviders can be used to access contact information, pictures, etc. stored on the device.

The query string used in the ContentProvider is different from the standard SQL query. Many operations such as select, add, delete and modify use a special URL. This URL consists of three parts, "content: / /", which represents the path of data and an optional ID representing data.

content://media/internal/images This URL will return all the pictures stored on the device

content://contacts/people/ This URL will return all contact information on the device

content://contacts/people/45 This URL returns a single result (contact record with ID 45 in contact information)

If you want to store byte data, such as bitmap files, the data column that stores the data is actually a URL string that represents the actual saved file. The client uses it to read the corresponding file data. The ContentProvider that processes this data type needs to implement a file named_ Data field_ The data field lists the exact path of the file on the Android file system. This field is used not only by the client, but also by the contentresolver. The client can call the contentresolver. Openoutputstream() method to process the file resource pointed to by the URL. If it is the contentresolver itself, it can directly access the data file because it holds higher permissions than the client.

2、 Method of use

Most content providers use the Android file system or SQLite database to hold data, but they can also store it in any way. This example uses SQLite database to maintain data.

1. Create an interface and define an interface named content_ URL, which is a public static final URI type class variable. You must specify a unique string value for it. The best solution is the full name of the class and the name of the data column.

2. Implement sqliteopenhelper

3. Create a class that inherits the parent class of ContentProvider

4. Use tags in androidmanifest.xml to set and call ContentProvider.

5. Add data

6. Delete data

7. Query data

8. Update data

3、 Small case

1. Add the strings.xml file

2. Modify activity_ Main.xml file

3. Add iprovidermetadata interface

4. Add contentproviderdbhelper class

5. Add bookcontentprovider class

6. Modify the androidmanifest.xml file

7. Modify mainactivity

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>