Android system content provide detailed

Source: Internet
Author: User

Content Provider is one of the components of an Android application, and as the only way to share data between applications, the main function of content Provider is to store and retrieve data and provide access to other applications as an excuse for accessing data.

The Android system has a series of Content Provider for common data types (such as music, video, images, mobile phone contact information, etc.) that are located under the Android.provider package. With a specific license, you can access these content Provider in your own development application.

There are two ways to share your data with other applications: creating your own content provier (that is, inheriting from a contentprovider subclass) or adding your own data to an existing content provider, which needs to be guaranteed Some content provider is the same as its own data type and has write permission to the content provider. For content Provider, the most important thing is the data model and URI.

1. Data Model

Content Provider The data stored in the form of a datasheet to the visitor, each of which is a record in the datasheet, each listed with a specific type and meaning. Each data record includes a "_id" numeric field that uniquely identifies a single piece of data.

2.URI

URI, each content Provider provides a public URI that uniquely identifies its own dataset (data set), and if a content Provider manages multiple datasets, it assigns a separate URI for each dataset. All of the content Provider URIs begin with "content://", where "content:" is used to identify the schema that the data is managed by the content Provider.

URIs are used in almost all content Provider operations, so in general, if you are developing your own content Provider, it is a good idea to define the URI as a constant, which improves the maintainability of your code while simplifying development.

To begin with, how to access the data in the content provider, the data in the content provider is accessed primarily through Contentresolver objects, and the Contentresolver class provides a member method that can be used to Provider data in the query, insert, modify and delete operations. For example, query a Content Provider need to master the following information.

URI that uniquely identifies the content Provider

The name of the data field that needs to be accessed.

The data type of the data field

Tip: If you need to access a particular data record, just the ID of the record.

There are two methods for querying the content provider: Contentresolver query () and the Managedquery () of the Activity object, both of which receive the same parameters and return the cursor object, the only difference being Using the Managedquery method allows the activity to manage the lifecycle of the Cursor.

The managed cursor calls its own Deactivate method to unload when the activity enters a paused state, while the activity returns to its run state by calling its own Requery method to query the generated cursor object again. If an cursor object wants to be managed by activity, the Startmanagingcursor method of the activity can be invoked to implement it.

An Android application can use a file or Sqllite database to store data. Content provider provides a way to share data among multiple applications, such as: contact information can be accessed by multiple applications. Content provider is a class that implements a set of standard methods for providing access to data for other applications.

The application can do the following in the content provider:

Querying data

modifying data

Add data

Delete data

Related Article

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.