/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/
1. Introduction to content provider
Literally, content providers are the content provider, that is, the data provider, and the data source can be SQLite database or file storage. To share data between applications, You can encapsulate the private data of applications into contentproviders, define a URI, and provide a unified data interface. Other applications can use this URI to access specified data and perform various operations, such as adding, deleting, querying, and updating ). Instead of operating the underlying private data of the application. Generally, the underlying private data is based on SQLite.
The general process of sharing data with other apps:
To facilitate the communication between other apps and the contentprovider of your applications, a common URI: content_uri is usually defined. Other apps communicate with contentprovider through this URI. It should be noted that after you build the contentprovider, You do not directly deal with it, but operate through contentresolver.
2. Use of content provider
2.1 use contentresolver to manage the content provider
Android provides contentresolver. External programs can use contentresolver to manage the data provided by contentprovider.
To put it simply, resolver is to break down and manage the provider's data content. You should know the URI before understanding the resolver. As a matter of fact, Uri is an address that corresponds to the data content of the content provider (for more information about Uri, see the end of the article ).
Contentresolver queries contentprovider through URI. contentresolver uses database-like operations (SQLite) to obtain data from content providers.
Contentresolver's main interface methods are as follows: (from the official Android Documentation)
| Return Value |
Function Declaration |
| Final URI |
Insert (uri url, contentvalues values) inserts a row into a table at the given URL. |
| Final int |
Delete (uri url, string where, string [] selectionargs) deletes row (s) specified by a content Uri. |
| Final cursor |
Query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) query the given Uri, returning a cursor over the result set. |
| Final int |
Update (URI Uri, contentvalues values, string where, string [] selectionargs) Update row (s) in a content Uri. |
To obtain contentresolver, you can call the getcontentresolver () method,
A. For example, the following shows the query method:
The preceding table shows that the return value of the query method is cursor. The Code is as follows:
ContentResolver cr = getContentResolver();Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Get cursor (cursor)(You can also use the. managedquery () method to obtain cursor. The two parameters are the same. The difference is that the second method can manage the life cycle of cursor.
For example, cursor Cr = managedquery (myperson, null, null );)
Why is it necessary to obtain the cursor? Because the data obtained through query is read through the cursor method. (Read only)
B. Other methods (pay attention to the returned values)
Similarly, for example, rename android to Apple. You can call the contenresolver. Update () method.
Delete a single record, call the contentresolver. Delete () method, and specify a specific row in the parameter.
To delete multiple records, call the contentresolver. Delete () method. Specify the content provider by using the URI parameter and add a where clause condition similar to SQL.
2.2. How to share the data of an application.
A. Create your own content provider (you must inherit the contentprovider class)
B. If the data of this application is consistent with the existing content provider data structure, after obtaining the "write" permission of this content provider,
Write data to an existing content provider.
/*************************************** **********************************/
1. Introduction to Uri
The Universal Resource Identifier ("Uri ").
Each type of resources available on Android, such as images, videos, address books, and text messages, can be represented by Uris.
Uri also includes URL and urn (for ease of understanding, the two are compared below)
The URL is easy to understand, that is, the webpage address.
As we all know, a URL can not only identify a specific website, but also a specific webpage.
Similarly, a URI not only identifies a specific content provider, but also identifies a specific database table in the content provider of the application.
The two are similar.
For more detailed comparison of the three, refer to this article
Http://www.eoeandroid.com/thread-526-1-1.html
2. Uri Components
A complete URI consists of four parts:
Divide it into a, B, c, d
"Content: // conowen. szu. mycontenprovider/tablename/#" # indicates the data ID
A: The general prefix "content: //". In this example, a content provider controls the data.
B: URI identifier, which defines which content provider provides the data. For third-party applications, to ensure the uniqueness of the URI identifier, it must be a complete, lowercase class name.
This identifier is described in the authorities attribute of the element: it is generally the name of the package. class that defines the contentprovider.
C: name of the table in the database (such as the address book)
D: If the URI contains the ID of the record to be retrieved, the data corresponding to the ID is returned. If there is no ID, all is returned. (for example, when reading the address book, id indicates a contact, and no ID indicates the entire address book)
The Android system provides two tool classes for Uri operations: urimatcher and contenturis.
Urimatcher
The urimatcher class is mainly used to match the URI.
Contenturis
The contenturis class is used to obtain the ID part after the URI path.