Android contentresolver Usage Instructions

Source: Internet
Author: User
Tags sqlite
How Android implements data sharing between applications. An application can expose its own data completely, it's not important to see how the data is stored by the application, or whether it's using a database or a file, or whether it's online, and it's important that the outside world can pass this set of standards and a unified interface with the data in this program, such as: Add (insert), delete (delete), query (queries), modify (update), of course, you need certain permissions.

How to expose the application's data. Android provides ContentProvider, a program can expose its own data completely by implementing an abstract interface with content provider, and content providers exposes data in a similar way to tables in the database. Content providers stores and retrieves data that allows all applications to access, which is the only way to share data between applications. To make your application's data public, there are 2 ways to create your own content provider or add your data to an existing content provider if you have the same data type and have write content Provider permissions.

How to obtain data exposed by other applications through a set of standard and unified interfaces. Android provides Contentresolver, and outside programs can access the data provided by ContentProvider via the Contentresolver interface.

The current article mainly explains how to get data shared by other applications, such as getting information from the Android phone book.

what is a URI.

Before learning how to get contentresolver, there is a noun that must be understood: the URI. A URI is the definition of a network resource, giving it a broader meaning in Android.

Divide it into a,b,c,d 4 parts:
A: Standard prefix, used to indicate that a content provider control the data, can not be changed;
B:uri identification, which defines which content provider provides this data. For a third-party application, to ensure the uniqueness of the URI identity, it must be a full, lowercase class name. This identification is described in the authorities attribute of the <provider> element:
<provider name= ". Transportationprovider "authorities=" Com.example.transportationprovider "... >
C: Path, Content provider uses these paths to determine what type of data is currently required, may not include the path in the URI, or it may include multiple;
D: If the URI contains, represents the ID of the record that needs to be fetched, and if there is no ID, returns all;
Because URIs are usually long and sometimes error-prone, it's hard to understand. So, there are some helper classes defined in Android, and some constants are defined in place of these long strings, for example: People.content_uri

Contentresolver Introduction Description

After reading these introductions, you must understand that Contentresolver is a URI to query the data provided in ContentProvider. In addition to URIs, you must know the name of the data segment you want to get, and the data type of the data segment. If you need to get a specific record, you must know the ID of the current record, which is the D part of the URI.

As mentioned earlier, the content providers is exposed to data in a similar way as a table in a database, so Contentresolver will also use a database-like operation to retrieve data from the content providers. Now briefly introduce the main interface of Contentresolver, as follows:
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, retur Ning a Cursor over the result set.
Final int Update (URI Uri, contentvalues values, String where, string[] selectionargs) update row (s) in a content Uri.

See here, whether feel the same as the operation of the database basically. This is the case, please refer to the explanation of the Android SQLite parsing in detail, not in this detailed explanation.

One last question: How to get contentresolver. Call Getcontentresolver (), for example: contentresolver CR = Getcontentresolver ();


Knowing that Contentresolver is contentprovider to get other data that is shared with the application, the interface between Contentresolver and ContentProvider should be similar.

Where ContentProvider is responsible for

* Organize the application data;
* Providing data to other applications;

Contentresolver is responsible for

* Obtain data provided by ContentProvider;
* Modify/Add/delete update data, etc.

ContentProvider is how to provide data to the outside world.

Android provides ContentProvider, a program that exposes its own data completely by implementing a ContentProvider abstract interface, and Contentproviders exposes data in a form similar to a table in the database, That means ContentProvider is like a "database". Then the external access to the data provided by it should be the same as the operation of obtaining data from the database, except that the URI is used to represent the external need to access the "database." As for how to identify what "database" is needed from the URI, this is what the Android bottom needs to do, not in detail. Brief analysis of the ContentProvider to provide external data operation interface:

Query (Uri, string[], String, string[], string)
Insert (Uri, contentvalues)
Update (Uri, Contentvalues, String, string[])
Delete (Uri, String, string[])

These operations and the operation of the database is basically the same, not in detail, the specific analysis can refer to the Android SQLite resolution of the detailed instructions. The place where special instructions are required is the URI:

The d part of the URI may contain a _id, which should appear in the SQL statement, and can appear in a special way, which requires that we need to pay extra attention to this particular information when providing the data. The Android SDK recommends that you include an ID in the supplied Data table field, and that the integer PRIMARY KEY autoincrement identifies the ID fields when the table is created.

how the ContentProvider organizes the data.

The organization data mainly includes: storing the data, reading the data, exposing the data in the way of the database. Data storage needs to be based on the requirements of the design, choose the appropriate storage structure, the preferred database, of course, you can choose other local files, or even data on the network. Data reading, exposing data in a database this requires that, regardless of how the data is stored, data must finally be accessed in the form of data.

There may be 2 more questions that need to be paid attention to.

1. When ContentProvider was created and who created it. To access the data shared by an application, you need to start the application. The problem is not explicitly stated in the Android SDK, but from the point of view of data sharing, the ContentProvider should have been created by the time the system started, otherwise there would be no data sharing. This requires a clear definition of the <provider> element in the Androidmanifest.xml.
2. It is possible for multiple programs to access a contentprovider at the same time through Contentresolver, and will not lead to "dirty data" like a database. This problem requires the synchronization of database access, especially the synchronization of data writes, when defining ContentProvider in Androidmanifest.xml, consider the value of the <provider> element multiprocess attribute On the other hand, Android provides the Notifychange () interface in Contentresolver, and notifies other contentobserver when the data changes, and this place should use the Observer mode, There should be some register,unregister-like interfaces in the Contentresolver.

So far, ContentProvider has been provided with a more comprehensive analysis of how to create ContentProvider, There are 2 ways to create a contentprovider of your own or add your data to an existing contentprovider, assuming that you have the same data type and have permission to write to the Content provider. Look at the source code in the Notepad specific example provided in the Android SDK sample.


Turn from: http://www.pin5i.com/showtopic-android-contentresolver-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.