When an application inherits the contentprovider class and overwrites the class to provide and store data, it can share its data with other applications.
The file operation mode. You can specify the file operation mode as context. mode_world_readable or context. mode_world_writeable to share data, but the data access mode varies depending on the data storage mode.
XML files are used to share data, and XML parsing is required to read and write data. sharedpreferences is used to share data, and the sharedpreferences API must be used to read and write data. The advantage of using contentprovider to share data is to unify the data access mode.
1. When using contentprovider to share data externally:
(1) 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 uri)}
The above method adds a contentprovider data sharing and return type to the addition, deletion, modification, and query.
(2) You must use <provider> to configure the contentprovider in androidmanifest. xml.
To allow other applications to find this contentprovider, contentprovider uses authorities (host name/Domain Name) to uniquely identify it. You can regard contentprovider as a website (the website is also a data provider ), authorities is his 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> Note: Once the application inherits the contentprovider class, the application is called contentprovider ). Contentprovider (content provider)'s scheme has been specified by Android, and scheme is: Content ://
The Host Name (or authority) is used to uniquely identify the contentprovider. External callers can find it based on this identifier.
The path can be used to indicate the data to be operated. The path construction depends on the business.
For example, to operate the record with the ID of 10 in the person table, you can build the path:/person/10.
Name field of the record whose ID is 10 in the person table to be operated, person/10/Name
To operate on all records in the person table, you can create a path like this:/person
To operate records in the xxx table, you can build the path:/xxx
Of course, the data to be operated may not come from the database, but also from other storage methods such as files, XML, or networks, as follows:
To operate on the Name node under the person node in the XML file, you can build the path:/person/Name
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 ")
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. (1) Introduction to the urimatcher class:
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 the URI to be matched. If the URI matches, a matching code is returned. // If the match () method matches content: // CN. itcast. provider. personprovider/person/23 path, returns a matching code of 2
Smatcher. adduri ("CN. itcast. provider. personprovider "," person/# ", 2); // # It is the 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 the path content: // CN. itcast. provider. personprovider/person is matched, and the returned matching code is 1.
(2) The contenturis class introduces the contenturis class used to obtain the ID part after the URI path. It has two practical methods:
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.
2. Use contentprovider to share data
Main Methods of the contentprovider class:
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 start with vnd. Android. cursor. DIR.
For example, to obtain all the person records, the URI 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 start with vnd. Android. cursor. Item.
For example, obtain the person record with the ID of 10. The URI is content: // CN. itcast. provider. personprovider/person/10, the returned mime-type string should be: "Vnd. android. cursor. item/person ".
3. 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 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.
Use contentresolver to add, delete, modify, and query data in contentprovider:
Contentresolver resolver = getcontentresolver ();
Uri uri = URI. parse ("content: // CN. itcast. provider. personprovider/person ");
// Add a record
Contentvalues values = new contentvalues ();
Values. Put ("name", "itcast ");
Values. Put ("Age", 25 );
Resolver. insert (Uri, values );
// Obtain all records in the person table
Cursor cursor = resolver. Query (Uri, null, "personid DESC ");
While (cursor. movetonext ()){
Log. I ("contenttest", "personid =" + cursor. getint (0) + ", name =" + cursor. getstring (1 ));
// Change the name field value of the record whose ID is 1 to liming.
Contentvalues updatevalues = new contentvalues ();
Updatevalues. Put ("name", "liming ");
Uri updateiduri = contenturis. withappendedid (Uri, 2 );
Resolver. Update (updateiduri, updatevalues, null, null );
// Delete the record with ID 2
Uri deleteiduri = contenturis. withappendedid (Uri, 2 );
Resolver. Delete (deleteiduri, null, null );
4. Monitor Data changes in contentprovider
If the visitor of contentprovider needs to know that the data in contentprovider has changed, you can call getcontentresolver (). policychange (Uri, null) when the data in contentprovider changes to notify the visitor registered on this URI.
Example:
Public class personcontentprovider extends contentprovider {
Public URI insert (URI Uri, contentvalues values ){
DB. insert ("person", "personid", values );
Getcontext (). getcontentresolver (). policychange (Uri, null );
}
}
If the visitor of contentprovider needs to be notified of data changes, you must use contentobserver to listen to the data (the data is described in the URI). When the notification of data changes is received, the system will call the onchange () method of contentobserver:
Getcontentresolver (). registercontentobserver (URI. parse ("content: // CN. itcast. providers. personprovider/person "),
True, new personobserver (new handler ()));
Public class personobserver extends contentobserver {
Public personobserver (handler ){
Super (handler );
}
Public void onchange (Boolean selfchange ){
// Here you can perform corresponding business processing
}
}
About the source code: I also paste it directly. There are many tests.
Address (both are): http://download.csdn.net/detail/u012301841/7618811
Http://download.csdn.net/detail/u012301841/7618817