10-Day Learning Android Development (3-2)-component ContentProvider

Source: Internet
Author: User

1)ContentProviderIntroduction
This class is used to provide data and methods for storing data, and it is possible to share its data with other applications. Although the use of other methods can also share data, but the way data access will vary depending on how the data is stored, such as: the use of file-based data sharing, the need for file operations read and write data;sharedpreferencesto share data, you need to usesharedpreferences APIread and write data. and usingContentProviderthe benefit of sharing data is that it unifies the way data is accessed.

2)eachContentProviderall have a publicURI, thisURIused to represent thisContentProviderThe data provided. Androidprovided by theContentProviderare stored inAndroid.providerPackage,Uriclass,such as:

Uri uri = uri.parse ("Content://com.changcheng.provider.contactprovider/contact")

3)ContentProviderthe methods provided
Query: Query
Insert: Inserting
Update: Update
Delete: Delete
GetType: Get Data type
OnCreate: callback function to invoke when creating data

in the Content Provider the query string used in this is different from the standard SQL Enquiry

Case: read the contact data as an example

    1. Create a new project, first in to set the ability to read contacts in Androidmanifest.xml:

Permission in ' Add ', UserPermission

Android.permission.READ_CONTACTS

actually Androidmanifest.xml added a line:

<uses-permissionandroid:name="Android.permission.READ_CONTACTS"/>

2) Read the name of the contact person

protectedvoid onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Activity_main );

      Cursor c=getcontentresolver (). Query (contactscontract.contacts. null null null null

while(C.movetonext ()) {System. out . println (">>>>>>>>>"+c.getstring (C.getcolumnindex ( Contactscontract.contacts. Display_name )));

}

Reference:

Http://www.cnblogs.com/devinzhang/archive/2012/01/20/2327863.html

http://xiechengfa.iteye.com/blog/1415829


This article is from "Blue Sea Tactics" blog, please make sure to keep this source http://wanxl.blog.51cto.com/2129901/1592776

10-Day Learning Android Development (3-2)-component 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.