Getting Started with Android development (20) content provider 20.1 data sharing

Source: Internet
Author: User
Tags data structures

In the previous section, we covered several methods for data persistence: preferences, files, and databases. It is recommended that you use Sqlitedatabase when you are saving complex data structures. However, sharing data is a challenge because the database is only available for packages that create it.

In this section, you'll introduce Android-specific ways to share data: using ContentProvider. It also describes how to use the built-in ContentProvider and create your own contentprovider to share data among multiple packages.

In Android, the recommended way to share data is to use ContentProvider. You can think of ContentProvider as a data store. How it stores data is irrelevant to the program that uses it. The important thing is how to get the data through the coding interface.

ContentProvider behaves very much like a database, you can perform additions and deletions to check the operation.

However, unlike a database, it can store data in different ways. Data can be stored in a database, in a file, or even on a network.

Android offers a number of useful contentprovider, as follows:

Browser stores browser bookmarks, browsing history, and more.

Calllog storage missed calls, call records, and so on.

Contacts Store contact details.

Mediastore stores media files, such as audio, video, pictures, etc.

Some configuration information for the Settings storage device.

In addition to these built-in contentprovider, you can create your own contentprovider.

To query for a contentprovider, you need to specify a query string in the form of a URI, specifying a row. The URI query form is as follows:

<standard_prefix>://<authority>/<data_path>/<id>

The components of the URI are as follows:

The prefix of the ContentProvider standard is content://

Authority specified the name of the ContentProvider. Contacts is the name of the built-in contacts ContentProvider. For third party ContentProvider, this authority may be a completed named name, such as Net.manoel.provider or Net.horsttnann.provider.

DATA_PATH Specifies the category of the request data. For example, if you are getting all the contacts in Contacts ContentProvider, then data_path should be people, that is, the URI would be: content://contacts/people

The ID specifies the requested record. For example, if you are looking for a contact number 2nd in Contacts ContentProvider, then the URI will be: CONTENT://CONTACTS/PEOPLE/2

Here are some examples of queries:

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.