content Provider is primarily used to implement data sharing among different applications , providing a complete set of mechanisms that allow one program to access data from another program while guaranteeing the security of the data being accessed. Currently, using a content provider is a standard way for Android to share data across programs.
Unlike two global read-write modes in file storage and sharepreferences storage, content providers can choose to share only what part of the data, ensuring that the privacy data in our programs is not compromised.
To access data in other programs:
For each application, if you want to access the data that is shared in the content provider, you must use the Contentresolver class to get an instance of the class through the Getcontentresolver () method in the context.
Contentresolver provides a series of methods for adding and removing data, where the Insert () method is used to add data, the update () method is used to update the data, the Delete () method is used to delete the data, and the query () method is used to query the data. They operate in a similar way to sqlitedatabase, except that they differ slightly in the method parameters.
Different additions and deletions in sqlitedatabase,contentresolver are not accepted by the table name parameter, but instead use a URI parameter, which is called the content URI. The content URI establishes a unique identifier for the data in the content provider, which consists mainly of two parts, the permission (authority), and the path. Permissions are used to differentiate between different applications and are typically named in the same way that the package name is used to avoid conflicts. Paths are used to differentiate between different tables in the same application, and are usually added to the back of the permission.
For example, if a program's package name is Com.example.app, the program's corresponding permissions can be named Com.example.app.provider. In the program's database there are two tables, table1 and table2, then the path can be named/table1 and/table2, and then the permissions and paths are combined, the content URI becomes com.example.app.provider/ Table1 and Com.example.app.provider/table2, and finally a protocol declaration is added to the header of the string. Therefore, the most standard format for content URIs is as follows:
Content://com.example.app.provider/table1
Content://com.example.app.provider/table2
The content URI can express very clearly the data in which program we want to access which table. It is also true that the Contentresolver method receives the Uri object as a parameter. After you get the content URI string, you also need to parse it into a URI object before it can be passed in as a parameter. Just call the Uri.parse () method to parse the content URI string into a URI object.
Uri uri = uri.parse ("Content://com.example.app.provider/table1")
Now we can use this Uri object to query the data in the Table1 table, as shown in the code below:
cursor cursor = getcontentresolver (). Query (URI, projection, selection, Selectionargs, SortOrder);
These parameters are similar to those in the query () method in Sqlitedatabase, but overall it is simpler, after all, when accessing data from other programs, there is no need to construct overly complex query statements.
The query () method parameter describes:
URI: Specifies that a table under one application is queried, corresponding to the from table_name
Projection: Specifies the column name of the query, corresponding to the select Column1,column2
Selection: Specifies the Where's constraint, corresponding to where column = value
Selectionargs: Provides a specific value for a placeholder in the where
ORDER BY: Specifies how the query results are sorted, corresponding to the ordered by column
When the query is complete, it is still a cursor object, and we can read the data from the cursor object one at a time. The idea of reading is still to traverse all the cursor rows by moving the cursor's position, and then take out the corresponding data for each row.
Master the most difficult query operations, the rest of the addition, modification, delete operation is even more.
Summarize:
The basic operations in Contentresolver are similar to those in Sqlitedatabase, except that Contentresolver does not receive the table name as a parameter, but instead uses the content URI to locate the specific program and data table, as in the case of Sqlitedatabase. The content URI is composed of "content://" + permission + path, and the content URI string is parsed into a URI object through the Uri.parser () method.
If you look harder, you still need to review the SQLite database store for sqlitedatabase:android data storage
Android to share data across programs, explore content providers