Summary of Android content provider ContentProvider

Source: Internet
Author: User

This article contains the following knowledge points:

ContentProvider
Introduction to URIs
Contentresolver:

Monitor the ContentProvider data changes

One: ContentProvider part

The role of ContentProvider:

  Content providers store and retrieve data and make it accessible to all applications. They ' re the only-to share data across applications;

There ' s no common storage area The all Android packages can access.

A content provider is a unique interface for sharing its data as an application for access by other applications. For example, I develop a program: If you want to access a contact, you have to get the data through the ContentProvider in the contact person.

And if other applications want to access my data, you have to look at my personal wishes, and I intend to create what ContentProvider are willing to share what data, others can see the data. Can be understood as my intention to pass

ContentProvider to expose my data.

ContentProvider in the form of data storage:

  Content providers expose their data as a simple table in a database model, where each row is a record and each column is D ATA of a particular type and meaning.

Exposing data in tabular form:

For example, information on people and their phone numbers might be exposed as follows:

  

Every record includes a numeric _ID field that uniquely identifies the record within the table. Each record will have a unique ID for the index data.


ContentProvider creation: (Note Be sure to configure in the manifest file after ContentProvider is created)

The steps to create are as follows:

  

The explanation in Chinese is:

1. Find a way you like to store your data.

2. Inherit the ContentProvider class, note: When inheriting this class, rewrite the following six methods for invocation:

    query()Query data, the result set is returned with the cursor
    insert()Inserting data
    update()Update data
    delete()Delete the data. The first parameter of the above four methods is passed to a URI object.
    getType()
    onCreate()

3. Configure in the manifest file://define yourself a contentprovider be sure to tell someone else, how else would they know?

      To let the Android system know on the content provider you ' ve developed,

Declare it with an <provider> element in the application ' s Androidmanifest.xml file.

Content providers that is not declared in the manifest is not visible to the Android system


      

The configuration also has an important parameter: exported, indicates whether the provider object can be accessed by other programs, of course, is true;

Note: The above configuration is the simplest configuration, as long as everyone knows the provider URI can be directly accessed, the actual may need to add permission permissions to improve permissions.

Two: the URI section:

A URI is a bridge that implements data sharing, which is a unique identifier for an Android resource, just like a URL in the web.

PublicStaticfinalUri=
Uri. Parse("Content://com.example.codelab.transportationprovider");

    

Three: Contentresolver:

The acquisition and use of ContentProvider should be combined with: Contentresolver and URIs to work together:
Forexample: Call the contact in my application:

    Contentresolver cr = getcontentresolver();

Gets the resolver object of the system, noting that you do not know which provider data to manipulate at this time.

A URI parameter is required to match the parameter when it is called

    

=                  Uri.  Parse("Content://com.example.codelab.transportationprovider");     

Cr can call the method of adding and deleting, and the parameters of the URI are actually calling the method of adding and deleting the corresponding contentprovider.

Four: Content Observer: Contentobserver:

When the data is shared, the data in provider can be changed by means of the relevant methods, and Android provides the content observer Contentobserver to listen for changes in its data.

In practical use, we need to inherit the Contentobserver class and rewrite the OnChange () method to listen for changes in the data.

To listen for data changes for the specified ContentProvider, you also need to register the content watcher listener with Contentresolver to the specified URI:

  

Parameter description: 1.uri specifies ContentProvider. 2.boolean value: Assume: Uri=content://abc, if True, then: CONTENT://ABC/D data changes will trigger the listener,

If flase: It will not be triggered.

3 Custom Contentobserver.

Summary of Android content provider ContentProvider

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.