Detailed Android ContentProvider and URIs

Source: Internet
Author: User

Sharing data using ContentProvider (content provider)

ContentProvider's role in Android is to share data externally, which means that you can share data from the application to other applications via ContentProvider, and other applications can be accessed through ContentProvider the data in your application for deletion. As for data sharing, we used to learn about file manipulation patterns, knowing that you can share data externally by specifying the operation mode of a file as Context.mode_world_readable or context.mode_world_writeable. So why use ContentProvider to share data externally? Is this, if the use of file operating mode to share data, data access will be different from the way the data is stored, resulting in access to data can not be unified, such as: the use of XML files to share data externally, the need for XML parsing to read data ; Share data with sharedpreferences, and you need to read data using the Sharedpreferences API.

The benefit of using ContentProvider to share data externally is to unify the way data is accessed.

When an application needs to share data externally through ContentProvider, the first step is to inherit ContentProvider and rewrite the following methods:

Public 

class Personcontentprovider extends contentprovider{public
boolean onCreate () public
Uri Insert ( Uri Uri, contentvalues values) public
int Delete (URI Uri, String selection, string[] selectionargs) public
int UPD Ate (URI Uri, contentvalues values, String selection, string[] selectionargs) public
Cursor query (Uri URI, string[] Pro Jection, string selection, string[] Selectionargs, 

string sortOrder) public
string GetType (Uri uri)
}

The second step is to configure the ContentProvider in Androidmanifest.xml using <provider>, in order for other applications to find the ContentProvider, ContentProvider used authorities (hostname/domain name) to its unique identification, you can take ContentProvider as a website (think, the site is also providing data), authorities is his domain name:

<manifest >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<provider android:name= ". Personcontentprovider "
android:authorities=" Com.ljq.providers.personprovider "/>
</application >
</manifest>

Ii. description of the URI

The URI represents the data to be manipulated, and the URI contains two pieces of information: 1 ContentProvider, 2, which operates on what data in ContentProvider, a URI consists of the following parts:

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.