Contentprovider introduction and Benefits
Introduction:
The role of contentprovider in Android is to share data externally, that is, you can share the data in the application to other applications through contentprovider for access. Other applications can use contentprovider to add, delete, modify, and query data in your application. For data sharing, we have learned the file operation mode before. We know that the operation mode of a specified file is context. mode_world_readable or context. mode_world_writeable. So why use contentprovider to share data externally? If the file operation mode is used to share data externally, the Data Access Mode varies depending on the data storage mode, resulting in inconsistent data access methods, such: if an XML file is used to share data, XML parsing is required to read the data. If sharedpreferences is used to share data, sharedpreferences must be used.
API to read data.
Benefits:
The advantage of using contentprovider to share data externally is to unify the data access mode.
When the application needs to share data externally through contentprovider,The first step is to inherit contentprovider and override the following method:
Public class personcontentprovider extends contentprovider {
Public Boolean oncreate ()
Public URI insert (URI Uri, contentvalues values)
Public int Delete (URI Uri, string selection, string [] selectionargs)
Public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs)
Public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder)
Public String GetType (URI )}
Step 2: Use <provider> to configure the contentprovider in androidmanifest. xmlTo allow other applications to find the contentprovider, contentprovider usesAuthorities (host name/domain name)You can identify contentprovider as a website (think about it as a data provider). Authorities is its domain name:
<Manifest...>
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Provider Android: Name = ". personcontentprovider" Android: Authorities = "cn. itcast. providers. personprovider"/>
</Application>
</Manifest>
Uri Introduction;
Uri indicates the data to be operated. Uri mainly contains two parts: 1. contentprovider to be operated; 2. what data is operated on in contentprovider, a URI consists of the following parts:
Contentprovider (content provider)SchemeAs specified by Android,Scheme is: Content ://
Host Name (or authority)It is used to uniquely identify the contentprovider, which can be found by external callers.
Path)It can be used to represent the data we want to operate on. The path construction should be based on the business, as shown below:
1.1 to operate records with the ID of 10 in the person table, you can build the path:/person/10
1.2 Name field of the record whose ID is 10 in the person table to be operated, person/10/Name
1.3 to operate all records in the person table, you can build the path:/person
1.4 to operate records in the xxx table, you can build the path:/xxx
1.5 The data to be operated may not come from the database, but may also be stored in files, XML, or the network, as follows:
To operate on the Name node under the person node in the XML file, you can build the path:/person/Name
1.6 if you want to convert a string to a URI, you can use the parse () method in the URI class as follows:
Uri uri = URI. parse ("content: // CN. itcast. provider. personprovider/person ")
Introduction to the urimatcher class:
Because URI represents the data to be operated, we often need to parse the URI and obtain data from the URI. The Android system provides two tool classes for Uri operations: urimatcher and contenturis. Understanding their usage will facilitate our development work.
The urimatcher class is used to match a URI. Its usage is as follows:
The first step is to register all the URI paths you need, as shown below:
// Constant urimatcher. no_match indicates that the return code does not match any path.
Urimatcher smatcher = new urimatcher (urimatcher. no_match );
// If the match () method matches the content: // CN. itcast. provider. personprovider/person path, the return matching code is 1.
Smatcher. adduri ("CN. itcast. provider. personprovider", "person", 1); // Add a URI to be matched. If yes, a matching code is returned.
// If the match () method matches the content: // CN. itcast. provider. personprovider/person/230 path, the return matching code is 2.
Smatcher. adduri ("CN. itcast. provider. personprovider", "person/#", 2); // # It is a wildcard
Switch (smatcher. Match (URI. parse ("content: // CN. itcast. provider. personprovider/person/10 "))){
Case 1
Break;
Case 2
Break;
Default: // Mismatch
Break;
}
After registering the URI to be matched, you can use smatcher. the match (URI) method matches the input URI. If it matches, the matching code is returned. The matching code is the third parameter passed in by calling the adduri () method. Assume that it matches the content: // CN. itcast. provider. personprovider/person path. The returned matching code is 1.
Contenturis class usage:
Withappendedid (Uri, ID) is used to add the ID part to the path:
Uri uri = URI. parse ("content: // CN. itcast. provider. personprovider/person ")
Uri resulturi = contenturis. withappendedid (Uri, 10 );
// The generated URI is: Content: // CN. itcast. provider. personprovider/person/10.
The parseid (URI) method is used to obtain the ID part from the path:
Uri uri = URI. parse ("content: // CN. itcast. provider. personprovider/person/10 ")
Long personid = contenturis. parseid (URI); // The returned result is: 10.
Use contentprovider to share data;
Public Boolean oncreate ()
This method is called after contentprovider is created. After Android is started, contentprovider is created only when other applications access it for the first time.
Public URI insert (URI Uri, contentvalues values)
This method is used by external applications to add data to contentprovider.
Public int Delete (URI Uri, string selection, string [] selectionargs)
This method is used for external applications to delete data from contentprovider.
Public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs)
This method is used by external applications to update data in contentprovider.
Public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder)
This method is used for external applications to obtain data from contentprovider.
Public String GetType (URI)
This method is used to return the MIME type of the data represented by the current URL. If the operated data belongs to the set type, the mime-type string should be vnd. android. cursor. DIR/. For example, the URI of all person records to be obtained is content: // CN. itcast. provider. personprovider/person, the returned mime-type string should be: "Vnd. android. cursor. DIR/person ". If the data to be operated belongs to a non-set type, the mime-type string should be vnd. android. cursor. start with item/. For example, you can obtain the person record with id 10 and the URI is content: // CN. itcast. provider. personprovider/person/10, the returned mime-type string should be: "Vnd. android. cursor. item/person ".
Use contentresolver to operate data in contentprovider:
When an external application needs to add, delete, modify, and query data in contentprovider, you can use the contentresolver class to complete the operation. To obtain the contentresolver object, you can use getcontentresolver () provided by activity () method. The contentresolver class provides four methods for the same signature as the contentprovider class:
Public URI insert (URI Uri, contentvalues values)
This method is used to add data to contentprovider.
Public int Delete (URI Uri, string selection, string [] selectionargs)
This method is used to delete data from contentprovider.
Public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs)
This method is used to update data in contentprovider.
Public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder)
This method is used to obtain data from contentprovider.
The first parameter of these methods is Uri, which indicates the contentprovider to be operated and the data to be operated on. Assume that the given parameter is Uri. parse ("Content: // CN. itcast. providers. personprovider/person/10), then the host name will be CN. itcast. providers. the contentprovider of personprovider performs operations. The operation data is the record with the ID of 10 in the person table.