Content provider for Android data sharing)

Source: Internet
Author: User

1. What is content provider:

Contentprovider: used to store and read data in multiple applications. This is the only way for Android to share data across applications. For example, audio, video, image, and private address book must be used in multiple applications.ProgramShare data between them. The advantage of using content provider to share data is to unify the data access mode (the system has provided the content provider of these main data types, which is under the Android. provider package ). If not, you can directly use the sqlitedatabase.

 

2. related data models:

Content Provider displays data similar to database tables:

1. Each record has a numeric field named _ ID (similar to the primary key of a database) to locate the specified record.

2. The returned result is a cursor object (similar to the JDBC resultset). You can use the method specified by cursor to read data of various types. Of course, you should first know the data type.

3. Each content provider defines a unique public URI to specify its dataset. A content provider can contain multiple datasets (multiple tables can be considered). In this way, multiple Uris must correspond to each dataset.

 

3. Uri

Uri indicates the data to be operated. Uri contains two parts:

(1). contentprovider to be operated.

(2). operate on the data in contentprovider.

A URI consists of the following parts:

Content: // com. example. transportationprovider/TRAINS/122

A B c d

A: Standard prefix, indicating that this URI specifies a content provider, which cannot be modified.

B: URI authority, indicating content provider. Third-party applications should have full class names (in lowercase) to ensure uniqueness. It is declared in the Authority attribute of the <provider> element.

C: Path: it can be used to indicate the data to be operated and determine the data to be requested.

D: ID of the specified record. Used to find a single row record, similar to a primary key.

The path construction should be based on the business, for example:

(1) to operate the record with the ID of 10 in the contact table, you can build the following path:/contact/10.

(2) to operate the name field of the record with the ID of 10 in the contact table, contact/10/Name
(3) to operate all records in the contact table, you can build the path:/contact

 

4. How to share data:

There are two ways to make your data public:

(1) Create your own content provider (inheriting the subclass of contentprovider)

(2) Add data to an existing provider (the data type is consistent with the corresponding permissions)

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.