Android Notes (54) Android One of the four components--contentprovider (i)

Source: Internet
Author: User

ContentProvider Provide data

In Android, each of his applications are independent of each other, running in their own Dalvik virtual machine, but the reality of use often need to exchange data between multiple applications, such as texting need to get contacts in the content, QQ upload avatar need to get album content, etc. Android provides a standard contentprovider for this cross-application data exchange, and when users implement their own contentprovider, they need to implement the abstract methods that he prescribes:

Public Boolean onCreate (): This method is called when ContentProvider is created, and is created when the other app accesses ContentProvider for the first time. And immediately callback the OnCreate () method.

Public URI Inert (URI uri,contentvalues values): inserts values corresponding to the data according to the URI.

public int Delete (URI uri,string selection,string[] selectionargs): deletes all records that match the select condition based on the URI.

public int Update (URI uri,contentvalues values, String selection,string[] selectionargs): modifies select criteria based on URI All the records that match

Public Cursor query (Uri URI, string[] projection,string selection, string[] selectionargs,string sortOrder) : All records matching the select condition are queried according to the URI, where projection is the list of columns indicating that only the specified data column is selected.

Public String gettypt (URI uri) : This method returns the MIME type of the data represented by the current URI, and if the URI corresponding data may include more than one data, the MIME type string should start with vnd.android.cursor.dir/ . If the data for the URI contains only one record, the return MIME type string should start with vnd.android.cursor.item/ .

What is a URI

Each of the above methods has a URI parameter, so what is the URI?

You can refer to these two posts for the URI http://www.cnblogs.com/gaojing/archive/2012/02/04/2413626.html

Http://www.cnblogs.com/hust-ghtao/p/4724885.html

In terms of the above two URLs:

http://This is the protocol part of the URL, as long as the HTTP protocol to access the site, preceded by this prefix.

www.cnblogs.com This is the domain name, access to the specified website, the domain name part is unchanged

gaojing/archive/2012/02/04/2413626.html and hust-ghtao/p/4724885.html This is the Site Resources section, this is a dynamic adaptation, Visitors can access different resources, and this section will be different.

ContentProvider requires a similar URI and URL, such as

Content://org.crazyit.providers.dictprovider/words

content :// This is the ContentProvider of Android, just like/HTTP, the default protocol to provide ContentProvider and access ContentProvider is content://

Org.crazyit.providers.dictprovider This is similar to the URL, the system through this name to find out which contentprovider you want to operate.

words This is the Resources section, where visitors visit different resources, this is also a dynamic change.

You can use the static method of the URI parse () to convert the string to a URI.

Contentresolver access to use

Commonly used in conjunction with ContentProvider is Contentresolver, where one application uses ContentProvider to provide data, and another application uses Contentresolver to access data.

You can use the Getcontentresolver () method of the context to get the ContentProvider object

After you have obtained the Contentresolver object, you can manipulate the data using the following methods:

Insert (URI uri,contentvalues values): inserts the data for the values corresponding to the URI corresponding to the ContentProvider.

Delete (URI uri,string where,string[] selectionargs): Delete the corresponding contentprovider in the URI where the matching data is submitted.

Update (Uri uri,contentvalues values,string where, string[] selectionargs): Updates the URI corresponding to the contentprovider where the matching data is submitted.

query (Uri uri,string[] projection,string selection,string[] selectionargs,string sortOrder): The query URI corresponds to the contentprovider where the matching data is submitted.

In general, ContentProvider is a singleton pattern, and when multiple applications operate through Contentresolver to manipulate the data provided by ContentProvider, Data operations that are called by Contentresolver will be delegated to the same contentprovider processing.

Register ContentProvider

After defining the ContentProvider, you also need to register the ContentProvider in the system, by adding a <provider> element to the Androidmainifest.xml file, it is important to note that The value of exported that we want to set for this element is true, indicating that this contentprovider can provide data to an external application

Android Notes (54) Android One of the four components--contentprovider (i)

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.