Contentprovider of Android learning notes

Source: Internet
Author: User

How does Android achieve data sharing between applications? An application can completely expose its own data, which is invisible to the outside world. It does not need to see how the data exposed by the application is stored, or whether it uses databases or files, or through the Internet, all these are not important. What is important is that the outside world can use this set of standards and unified interfaces to deal with the data in this program, for example: add (insert) delete, query, and update require certain permissions.

How to expose application data? Android provides contentprovider. A program can completely expose its data by implementing an abstract interface of the content provider, and content providers exposes data in a way similar to a table in a database. Content Providers stores and retrieves data, which can be accessed by all applications. This is the only way to share data between applications. To make the application data public, you can create a content of your own in two ways:
Provider or add your data to an existing content provider, provided that you have the same data type and the permission to write to the content provider.

How can I obtain data exposed by other applications through a set of standard and unified interfaces? Android provides contentresolver, and external programs can access the data provided by contentprovider through the contentresolver interface.


What is uri?


Before learning how to obtain contentresolver, you must know the following terms: URI. Uri is the definition of network resources. It is given a broader meaning in Android. Let's take a look at the following example:

Divide it into four parts: A, B, C, and D:
A: The standard prefix. It cannot be changed if 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 <provider> element:
<Provider name = ". transportationprovider" authorities = "com. example. transportationprovider"...>
C: path. content provider uses these paths to determine the type of data to be generated. The URI may not include paths or multiple data types;
D: If the URI contains the ID of the record to be retrieved; if there is no ID, all records are returned;
Because the URI is usually long and sometimes error-prone, it is hard to understand. Therefore, some helper classes are defined in Android and some constants are defined to replace these long strings, such as: people. content_uri

Contentresolver

Contentresolver queries the data provided in contentprovider through Uri. In addition to Uri, you must also know the name of the data segment to be obtained and the Data Type of the Data Segment. If you need to obtain a specific record, you must know the ID of the current record, that is, the D part of the URI.

As mentioned above, content providers exposes data in a way similar to tables in a database, so contentresolver will also use database-like operations to obtain data from content providers. The following describes the main interfaces of contentresolver:

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.


How does contentprovider provide external data?

Android provides contentprovider. A program can completely expose its data by implementing an abstract interface of contentprovider, and contentproviders exposes data in a way similar to a table in a database, that is to say, contentprovider is like a "Database ". The data provided by the outside world should be basically the same as the data obtained from the database, except that the URI is used to indicate the "Database" that the outside world needs to access ". As for how to identify which database is needed from the URI, this is what the android underlying layer needs to do. In brief, contentprovider provides external data operation interfaces:

Query (Uri,
String [], String, string [], string)
Insert (Uri,
Contentvalues)
Update (Uri,
Contentvalues, String, string [])
Delete (Uri,
String, string [])
The D part of URI may contain a _ id, which should appear in the SQL statement and can appear in a special way. This requires that when we provide data, you need to pay attention to this special information. The recommended method for Android SDK is to include an ID in the provided data table field. During table creation, integer primary key autoincrement identifies this ID field.

Contentprovider
How is data organized?

Organization data mainly includes data storage, Data Reading, and data exposure in the form of a database. Data storage needs to select the appropriate storage structure based on the design requirements, the preferred database, of course, you can also select other local files, or even data on the network. When reading data, exposing data in the form of a database requires that no matter how the data is stored, the data must be accessed in the form of data.

There may be two other issues that need attention.

1. When was contentprovider created? who created it? Do I need to start this application to access the data shared by an application? This issue is not explicitly stated in the android SDK, but from the perspective of data sharing, contentprovider should be created when Android is started, otherwise data sharing will not be discussed. Therefore, the <provider> element must be clearly defined in androidmanifest. xml.
2. Multiple programs may access a contentprovider through contentresolver at the same time. Will it lead to "dirty data" like the database "? This problem requires synchronization of database access, especially data writing, in androidmanifest. when defining contentprovider in XML, consider the value of the <provider> element Multiprocess attribute. In addition, Android provides
The yychange () interface notifies other contentobserver when data changes. The observer mode should be used in this place. In contentresolver, there should be some interfaces similar to register and unregister.


Contentprovider

* Organize application data;
* Provide data to other applications;

Contentresolver is responsible

* Obtain the data provided by contentprovider;
* Modify, add, or delete updated data;

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.