The content resource ID is the unified identifier of the data in the provided device. Content resource identifiers include the complete provider's name (its permissions) and the name (a path) pointing to the table or file ). The optional ID part points to an independent row in the table. Each data access method of the contentprovider class must have a content resource ID as a parameter, which allows you to decide the table, row, or file to be accessed.
Design permission
Generally, the provider has a single permission as its name in Android. To avoid conflicts with other providers, you should use Internet domain ownership as the basis for your providers' permissions. Because this recommendation applies to the android package name, you can use an extension that contains the package name of the provisioner to define the permissions of your provisioner. For example, if your android package name is com. example. <appname>, you should be granted the permission of COM. example. <appname>. provider.
Design Path Structure
Generally, a developer creates a content resource identifier (URI) by attaching the path pointing to a single table to the permission ). For example, if you have two tables: Table1 and Table2, you can combine these two tables with the permissions in the preceding example to generate the following resource IDs:
Com. example. <appname>. provider/Table1 and COM. example. <appname>. provider/Table2. The path is not limited to the part of a single table. Not every path level requires a table.
ID of the processed content resource
Through the protocol, the provider can provide access to a single row in a table by accepting the content resource ID that carries the row id value at the end of the URI. It can also be the same as the protocol. The provider matches the id value with the _ ID column of the table and executes request access for matching rows.
This Protocol facilitates the adoption of a common design pattern for the application access provider. The application executes a query for the provided server and displays the result of the cursor object in the listview object using the cursoradapter object. The definition of cursoradapter requires that a column in the cursor object be _ id.
You can select a row from the row displayed in the UI to view or modify data. The application obtains the corresponding row mapped from the cursor object to the listview, obtains the _ id value of this row, attaches this _ id value to the resource ID, and sends this access request to the provider, then, the provisioner can query or modify the row selected by the user.
Content resource identification Mode
To help you select an appropriate action for the input content resource identifier, the content provider API includes the Protocol Class urimatcher, which maps the content resource identifier pattern to an integer. You can use this integer in the switch statement to select the desired action for the content resource ID or resource ID that matches the specified mode.
The following are the wildcards used in the resource identity (URI:
1. *: matches any length and any valid string;
2. #: match the string of any length.
As an example of designing and encoding content resource identity (URI) processing, a provider with the permission com. example. App. provider can identify the following resource IDs pointing to the table:
1. Content: // com. example. App. provider/Table1: point to a table named Table1;
2. Content: // com. example. App. provider/Table2/dataset1: point to a table named dataset1.
3. Content: // com. example. App. provider/Table2/dataset2: point to a table called dataset2;
4. Content: // com. example. App. provider/table3: point to a table named table3.
The provider also identifies the resource IDs that append row IDs at the end of the resource ID, for example:
Content: // com. example. App. provider/table3/1 specifies the row whose ID is 1 in table 3.
The following resource identification modes are also possible:
Content: // com. example. App. provider /*
This mode can match any resource ID in the provider.
Content: // com. example. App. provider/Table2 /*:
Content: // com. example. App. provider/table3 /#:
Matches a row in table 3, as shown in figure
Content: // com. example. App. provider/table3/6
The matched data is the row whose table 3 ID is 6.
The following code snippet shows how urimatcher's methods work. This Code uses the table resource identification mode content: // <authority>/<path> and the single-row resource identification mode content: // <authority>/<path>/<ID> to identify the resource ID of the processing table and the resource ID of a single row.
The adduri () method maps permissions and paths to an integer. Android. content. urimatcher # match (URI) match () method returns an integer to a URI. A switch statement selects between querying a single row record in the entire table box:
Public class exampleprovider extends contentprovider {
...
// Creates a urimatcher object.
Private Static final urimatcher surimatcher;
...
/*
* The callto adduri () go here, for all of the content URI patterns that the provider
* Shoshould recognize. For this snippet, only the callfor Table 3 are shown.
*/
...
/*
* Sets the integer value for multiple rows in Table 3 to 1. Notice that no wildcard is used
* In the path
*/
Surimatcher. adduri ("com. example. App. provider", "table3", 1 );
/*
* Sets the code for a single row to 2. In this case, the "#" wildcard is
* Used. "content: // com. example. App. provider/table3/3" matches,
* "Content: // com. example. App. provider/table3 doesn't.
*/
Surimatcher. adduri ("com. example. App. provider", "table3/#", 2 );
...
// Implements contentprovider. Query ()
Public cursor query (
Uri,
String [] projection,
String selection,
String [] selectionargs,
String sortorder ){
...
/*
* Choose the table to query and a sort order based on the Code returned for the incoming
* URI. Here, too, only the statements for table 3 are shown.
*/
Switch (surimatcher. Match (URI )){
// If the incoming Uri was for all of table3
Case 1:
If (textutils. isempty (sortorder) sortorder = "_ id ASC ";
Break;
// If the incoming Uri was for a single row
Case 2:
/*
* Because This URI was for a single row, the _ id value part is
* Present. Get the last path segment from the URI; this is the _ id value.
* Then, append the value to the WHERE clause for the query
*/
Selection = Selection + "_ id =" URI. getlastpathsegment ();
Break;
Default:
...
// If the URI is not recognized, you should do some error handling here.
}
// Call the code to actually do the query
}
Another contenturis class provides a convenient way to process content resource identifiers with IDs. The URI and Uri. Builder of this class contain convenient methods for parsing existing URI objects and creating new URI objects.